从 .Plist 加载数组

发布于 2024-11-28 12:28:43 字数 397 浏览 0 评论 0原文

我正在尝试从 .Plist 中的数组加载数组,但它不起作用。

plist 如下所示:

在此处输入图像描述

这是我正在使用的代码:

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

I'm trying to load my array from an array in my .Plist but its not working.

The plist looks like this:

enter image description here

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];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

掩耳倾听 2024-12-05 12:28:43

试试这个。请更改文件名。
效果很好。

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

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

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];
终弃我 2024-12-05 12:28:43

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

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).

瑶笙 2024-12-05 12:28:43
NSString *pathStr = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [settingsBundlePath stringByAppendingPathComponent:@"yourproject.plist"]; 
NSDictionary *settingsDict = [NSDictionary dictionaryWithContentsOfFile:finalPath];

只需检查 settingsDict 的描述即可。希望这对您有帮助。

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.

治碍 2024-12-05 12:28:43

如果您的 plist 文件位于应用程序包中,最简单的方法是:

NSArray *myArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"filename_without_extension" ofType:@"plist"]];

If your plist file is in your application bundle, the easiest is this:

NSArray *myArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"filename_without_extension" ofType:@"plist"]];
怎樣才叫好 2024-12-05 12:28:43

使用这个方法对我来说效果很好。

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

Use this method it worked fine for me.

NSArray *country= [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"CountryList" ofType:@"plist"]];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文