首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >initWithContentsOfFile:编码:error:- NSInvalidArgumentException

initWithContentsOfFile:编码:error:- NSInvalidArgumentException
EN

Stack Overflow用户
提问于 2011-12-08 15:37:46
回答 1查看 1.4K关注 0票数 1

我想我漏掉了一些非常基本的,但是它是这样的。

initWithContentsOfFile:在XCode中的内联帮助告诉我,

代码语言:javascript
复制
NSMutableDictionary *myDict = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];

相反,应该使用initWithContentsOfFile:encoding:error:

代码语言:javascript
复制
NSMutableDictionary *myDict = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath encoding:NSUTF8StringEncoding  error:NULL];

我的问题是,当下面的代码引发错误时,initWithContentsOfFile:工作得很好。

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

错误:

代码语言:javascript
复制
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'
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-12-09 19:41:55

正如克里斯向我指出的那样,NSDictionary正在按预期工作。

不过,当alt-单击XCode中的方法时,我仍然会收到下面的消息。奇怪的是,文档是为NSString而不是NSDictionary编写的。应该把我的阅读镜戴上,再次感谢克里斯指出答案!

代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8433497

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档