从.Plist加载数组

Jon*_*Jon 6 iphone xcode objective-c plist nsarray

我正在尝试从我的.Plist中的数组加载我的数组,但它无法正常工作.

plist看起来像这样:

在此输入图像描述

这是我正在使用的代码:

NSString *path = [[NSBundle mainBundle]pathForResource:@"DiseasePropertyList" ofType:@"plist"];
NSMutableArray *rootLevel = [[NSMutableArray alloc]initWithContentsOfFile:path];
self.myArray = rootLevel;
[rootLevel release];
Run Code Online (Sandbox Code Playgroud)

Cha*_*gUZ 9

试试这个.请更改文件名.它工作正常.

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *recentFilePath = [documentsDirectory stringByAppendingPathComponent:@"recent.plist"];
NSArray *history = [NSArray arrayWithContentsOfFile:recentFilePath];
Run Code Online (Sandbox Code Playgroud)

  • 另一种方式:`NSURL*url = [[NSBundle mainBundle] URLForResource:@"DiseasePropertyList"withExtension:@"plist"]; NSArray*myArray = [NSArray arrayWithContentsOfURL:url];` (2认同)

And*_*oth 5

你的plist实际上是一本字典.数组是键"Array"的字典对象.您可以通过选择"Array"并切割(CMD-x),然后粘贴到空的plist(CMD v)中,将plist放入Xcode 4中的数组中.

  • 我想说这很可能就是问题所在.您还可以将plist视为源,只需从文件中删除`<dict>`和`</ dict>`标记(我一直这样做). (2认同)
  • 还要在开始标记<dict>之后删除<key> .. </ key>标记..这肯定会解决我的问题.... (2认同)