从的.plist文件中加载一个NSArray [英] Loading an NSArray from a .plist file

查看:196
本文介绍了从的.plist文件中加载一个NSArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,我知道这个问题似乎真的基本的,但我不能在任何地方找到一个解决我的问题 -

OK, so I know this question seems really basic, but I can't find a solution to my problem anywhere -

我有我的项目名为感受内的.plist文件。我的plist文件内容如下:

I have a .plist file within my project entitled 'feelings'. My plist file reads:

<plist version="1.0">
<dict>
    <key>feelings</key>
    <array>
        <string>Happy</string>
        <string>Sad</string>
    </array>
</dict>
</plist>

在根据.m文件 - (无效)viewDidLoad中;我想用下面的code检索plist中的内容:

In the .m file under -(void)ViewDidLoad; I am trying to retrieve the contents of the plist using the following code:

NSString *path = [[NSBundle mainBundle] pathForResource:@"feelings" ofType:@"plist"];
NSArray *tempArray = [[NSArray alloc] initWithContentsOfFile:path];

self.feelingsArray = tempArray;
[tempArray release]; 

不过,我似乎没有被加载从文件的.plist阵列和执行以下code:

However I don't seem to be loading the array from the .plist file and executing the following code:

NSLog(@"feelings: %@", feelings); 

收益(空)

任何人都可以找出我要去哪里错了吗?这一切似乎没什么问题,从我所工作过的。

Can anyone figure out where I'm going wrong? It all seems fine to me, from what I've worked on before.

推荐答案

plist文件的顶层是一本字典,而不是一个数组。您应该使用的NSDictionary 而不是的NSArray

Your top-level of the plist file is a dictionary, not an array. You should use NSDictionary instead of NSArray:

NSDictionary *tempDict = [[NSDictionary alloc] initWithContentsOfFile:path];
self.feelingsArray = [tempDict objectForKey:@"feelings"];
[tempDict release];

这篇关于从的.plist文件中加载一个NSArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆