initWithContentsOfFile:编码:错误:-NSInvalidArgumentException

initWithContentsOfFile:编码:错误:-NSInvalidArgumentException,initwithcontentsoffile,invalidargumentexception,Initwithcontentsoffile,Invalidargumentexception,我想我在这里遗漏了一些非常基本的东西,但它就在这里 XCode中的内联帮助告诉我,initWithContentsOfFile:已弃用 NSMutableDictionary *myDict = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath]; 相反,应使用initWithContentsOfFile:encoding:error: NSMutableDictionary *myDict = [[NSMutableD

我想我在这里遗漏了一些非常基本的东西,但它就在这里

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'

正如Chris向我指出的,NSDictionary正在按预期工作

但是,当alt在XCode中单击该方法时,我仍然收到下面的消息。 奇怪的是,这些文档是针对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

正如Chris向我指出的,NSDictionary正在按预期工作

但是,当alt在XCode中单击该方法时,我仍然收到下面的消息。 奇怪的是,这些文档是针对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

你的目标是什么版本的IOS。据我所知,2.0版之后的所有内容都是[[NSMutableDictionary alloc]initWithContentsOfFile:filePath]。文件是。在NSDictionary的文档中没有提到initWithContentsOfFile:encoding:error,所以我会避开它。Thaks的回答!我正在准备iOS 4.3。我现在注意到,XCode警告是针对NSString的,尽管我正在使用NSDictionary。奇怪。再次感谢克里斯!你的目标是什么版本的IOS。据我所知,2.0版之后的所有内容都是[[NSMutableDictionary alloc]initWithContentsOfFile:filePath]。文件是。在NSDictionary的文档中没有提到initWithContentsOfFile:encoding:error,所以我会避开它。Thaks的回答!我正在准备iOS 4.3。我现在注意到,XCode警告是针对NSString的,尽管我正在使用NSDictionary。奇怪。再次感谢克里斯!