NSKeyedUnarchiver debugObjectForKey 返回 null 类
我有一个存档存储在“Presidents.plist”文件中。它存在,我可以使用 pathForResource 方法找到它。而且我也使用 NSData 的 initWithContentsOfFile 方法获取数据来检索数据。现在的问题就出在这行代码上。
NSString *path = [[NSBundle mainBundle] pathForResource:@"Presidents"
ofType:@"plist"];
NSData *data;
NSKeyedUnarchiver *unarchiver;
data = [[NSData alloc] initWithContentsOfFile:path];
unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
id presidents = [unarchiver decodeObjectForKey:@"Presidents"];
由于某种原因,它返回一个 null 类,而不是我预期的 NSArray 类。谁能告诉我发生这种情况的原因以及如何解决这个困境?
我的课是这样的...
NSLog(@"presidents class = %@", NSStringFromClass([presidents class]));
I have an archive which is stored in "Presidents.plist" file. It exist and I can locate it using pathForResource
method. And also I do get the data using initWithContentsOfFile
method of NSData
to retrieve the data. The problem now is in this lines of code.
NSString *path = [[NSBundle mainBundle] pathForResource:@"Presidents"
ofType:@"plist"];
NSData *data;
NSKeyedUnarchiver *unarchiver;
data = [[NSData alloc] initWithContentsOfFile:path];
unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
id presidents = [unarchiver decodeObjectForKey:@"Presidents"];
For some reason it is returning a null class instead of my expected NSArray class. Can anyone tell me the reason this is happening and how can I solve this dilemma?
I'm getting the class like this...
NSLog(@"presidents class = %@", NSStringFromClass([presidents class]));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以通过以下方式访问该对象:
archieverPath 是 plist 的路径。
你可以通过以下方式返回对象
You can access the object by:
archieverPath is the path of the plist.
and you can return the object by
在最新版本的 iOS 中,pList 的根对象不能是数组。我最近遇到了这个问题。尝试使用字典代替。
In more recent versions of iOS the root object of a pList must not be an array. I had this problem recently. Try using a dictionary instead.
我遇到了同样的问题,方法2帮助了我。
使用 TextWrangler 将 Presidents.plist 中的“BIDPresident”更改为“President”或尝试我的文件 http://www.fileswap.com/dl/o29Sxm48g0/Presidents.plist.html
I had the same problem, Method 2 helped me.
Change the "BIDPresident" to the "President" in Presidents.plist using TextWrangler or try my file http://www.fileswap.com/dl/o29Sxm48g0/Presidents.plist.html