简体   繁体   English

从.Plist加载数组

[英]Loading Array From .Plist

I'm trying to load my array from an array in my .Plist but its not working. 我正在尝试从我的.Plist中的数组加载我的数组,但它无法正常工作。

The plist looks like this: plist看起来像这样:

在此输入图像描述

This is the code I'm using: 这是我正在使用的代码:

NSString *path = [[NSBundle mainBundle]pathForResource:@"DiseasePropertyList" ofType:@"plist"];
NSMutableArray *rootLevel = [[NSMutableArray alloc]initWithContentsOfFile:path];
self.myArray = rootLevel;
[rootLevel release];

Try this. 尝试这个。 Please change file name. 请更改文件名。 It works fine. 它工作正常。

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

NSString *recentFilePath = [documentsDirectory stringByAppendingPathComponent:@"recent.plist"];
NSArray *history = [NSArray arrayWithContentsOfFile:recentFilePath];

Your plist is actually a dictionary. 你的plist实际上是一本字典。 The array is the object of that dictionary for key "Array". 数组是键“Array”的字典对象。 You can make the plist into an array in Xcode 4 by selecting "Array" and cutting (CMD-x), and then pasting into the empty plist (CMD v). 您可以通过选择“Array”并切割(CMD-x),然后粘贴到空的plist(CMD v)中,将plist放入Xcode 4中的数组中。

如果你的plist文件在你的应用程序包中,最简单的是:

NSArray *myArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"filename_without_extension" ofType:@"plist"]];
NSString *pathStr = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [settingsBundlePath stringByAppendingPathComponent:@"yourproject.plist"]; 
NSDictionary *settingsDict = [NSDictionary dictionaryWithContentsOfFile:finalPath];

and just check the description of settingsDict. 并检查settingsDict的描述。 Hope this help you. 希望这对你有所帮助。

使用这种方法它对我来说很好。

NSArray *country= [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"CountryList" ofType:@"plist"]];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM