xcode 从 .Plist 加载数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6977015/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Loading Array From .Plist
提问by Jon
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];
回答by ChangUZ
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];
回答by Andrew Toth
Your plist is actually a dictionary. The array is the object of that dictionary for key "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).
你的 plist 实际上是一本字典。该数组是该字典的键“数组”的对象。你可以在Xcode 4中通过选择“Array”并剪切(CMD-x),然后粘贴到空的plist(CMD v)中,将plist制作成一个数组。
回答by Erwan
If your plist file is in your application bundle, the easiest is this:
如果您的 plist 文件在您的应用程序包中,最简单的方法是:
NSArray *myArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"filename_without_extension" ofType:@"plist"]];
回答by Michael Bai
NSString *pathStr = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [settingsBundlePath stringByAppendingPathComponent:@"yourproject.plist"];
NSDictionary *settingsDict = [NSDictionary dictionaryWithContentsOfFile:finalPath];
and just check the description of settingsDict. Hope this help you.
只需检查settingsDict的描述。希望这对你有帮助。
回答by manishsharma93
Use this method it worked fine for me.
使用这种方法对我来说效果很好。
NSArray *country= [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"CountryList" ofType:@"plist"]];