1

ここでは非常に基本的なものが欠けていると思いますが、ここにあります。

XCodeのインラインヘルプは、initWithContentsOfFile:非推奨であることを教えてくれます。

NSMutableDictionary *myDict = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];

代わりに、initWithContentsOfFile:encoding:error:を使用する必要があります。

NSMutableDictionary *myDict = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath encoding:NSUTF8StringEncoding  error:NULL];

私の問題は、initWithContentsOfFile:が正常に機能する一方で、以下のコードでエラーが発生することです。

NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"prefs" ofType:@"plist"];
Load preferences into symbol dictionary
NSMutableDictionary *myDict = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath encoding:NSUTF8StringEncoding  error:NULL];

エラー:

2011-12-08 16:27:12.209  -[__NSPlaceholderDictionary initWithContentsOfFile:encoding:error:]: unrecognized selector sent to instance 0x4b2a370
2011-12-08 16:27:12.212 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSPlaceholderDictionary initWithContentsOfFile:encoding:error:]: unrecognized selector sent to instance 0x4b2a370'
4

1 に答える 1

1

クリスが私に指摘したように、NSDictionaryは期待どおりに機能しています。

ただし、XCodeでメソッドをAltキーを押しながらクリックすると、以下のメッセージが表示されます。奇妙なことに、ドキュメントはNSString用であり、NSDictionary用ではありません。私の読書用ゴーグルを装着する必要がありました。答えを指摘してくれたクリスにもう一度感謝します。

initWithContentsOfFile:
Initializes the receiver, a newly allocated NSString object, by reading data from the file named by path. (Deprecated in iOS 2.0. Use initWithContentsOfFile:encoding:error: or initWithContentsOfFile:usedEncoding:error: instead.)

- (id)initWithContentsOfFile:(NSString *)path
Discussion
Initializes the receiver, a newly allocated NSString object, by reading data from the file named by path. If the contents begin with a byte-order mark (U+FEFF or U+FFFE), interprets the contents as Unicode characters; otherwise interprets the contents as data in the default C string encoding. Returns an initialized object, which might be different from the original receiver, or nil if the file can’t be opened.

Availability
Available in iOS 4.0 and later.
Deprecated in iOS 2.0.
See Also
– initWithContentsOfFile:encoding:error:
– initWithContentsOfFile:usedEncoding:error:
Declared In
NSString.h
于 2011-12-09T19:41:55.913 に答える