将plist放入数组
我见过很多类似的问题,但没有任何对我有用。我需要将 plist 的内容放入 NSMutableArray 中。
我的 plist 是一个包含 1 个数组的字典。该数组有多个字典。我在另一个应用程序中使用了相同的代码并且它有效:
NSString *path = [[NSBundle mainBundle] pathForResource:@"TeamsData" ofType:@"plist"];
NSMutableArray *temp = [[NSMutableArray arrayWithContentsOfFile:path] retain];
NSLog(@"TEMP: %@", temp);
data = [NSMutableArray arrayWithArray:[[temp objectAtIndex:0] objectForKey:@"Teams"]];
temp 返回 NULL 但我不明白为什么。有什么想法吗?
更新
我实际上发现了为什么我的代码可以在一个应用程序中运行,而不能在另一个应用程序中运行。在应用程序中,当我打开 plist 作为源时,它确实可以工作,整个 plist 都包含在一个数组中。不过,当我在标准 xcode plist 编辑器中查看它时,它并没有显示出来。因此,为了让相同的代码在新的 plist 上工作,我可以将其作为源代码打开并将其包含在数组标签中。
I have seen lots of similar questions but nothing is working for me. I need to get the contents of my plist into an NSMutableArray.
My plist is a dictionary containing 1 array. This array has multiple dictionaries. I have used the same code in another app and it worked:
NSString *path = [[NSBundle mainBundle] pathForResource:@"TeamsData" ofType:@"plist"];
NSMutableArray *temp = [[NSMutableArray arrayWithContentsOfFile:path] retain];
NSLog(@"TEMP: %@", temp);
data = [NSMutableArray arrayWithArray:[[temp objectAtIndex:0] objectForKey:@"Teams"]];
temp returns NULL but I can't figure out why. Any ideas?
Update
I actually discovered why my code was working in one app and not the other. In the app it did work in when I opened the plist as source the entire plist was enclosed in an array. This didn't show up when I viewed it in the standard xcode plist editor though. So to get the same code working on the new plist I could just open it as source and enclose it in array tags.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你说你有一个 plist,它是一个包含字典数组的字典,但你尝试从包中读取文件,就好像它是一个数组一样。尝试使用
NSDictionary
的-initWithContentsOfFile:
代替。该数组应该在 plist 字典中具有一个键,如下所示:
因此,提取数组应该是向 plist 字典询问带有键
Teams
的对象:You say you have a plist which is a dictionary containing an array of dictionaries, but you try to read the file from the bundle as if it were an array. Try using
NSDictionary
's-initWithContentsOfFile:
instead.The array should have a key in the plist's dictionary, something like the folllowing:
So extracting the array should be a matter of asking the plist dictionary for the object with key
Teams
:别紧张 :)
Take it easy :)