无法将 plist 加载到 NSArray 中

发布于 2024-12-24 18:59:33 字数 401 浏览 2 评论 0原文

我正在尝试将以下 plist 加载到 NSArray 中:

在此处输入图像描述

这是我用来执行的代码加载数据:

// Load the data from the plist file
NSString *filepath = [[NSBundle mainBundle] pathForResource:@"MyData" ofType:@"plist"];
NSArray *myArray = [[NSArray alloc] initWithContentsOfFile:filepath];

但是,当我在加载数据后放置断点时,我注意到数组是空的。请有人帮助我了解出了什么问题?

I'm trying to load the following plist into an NSArray:

enter image description here

Here is the code I'm using to load the data:

// Load the data from the plist file
NSString *filepath = [[NSBundle mainBundle] pathForResource:@"MyData" ofType:@"plist"];
NSArray *myArray = [[NSArray alloc] initWithContentsOfFile:filepath];

However when I place a breakpoint after the data has been loaded I notice that the array is empty. Please can someone help me understand what's going wrong?

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

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

发布评论

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

评论(3

街道布景 2024-12-31 18:59:34

您的 plist 文件有一个包含数组的字典。试试这个

NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:filePath];
NSArray *array = [[NSArray alloc]initWithArray:[dict objectForKey:@"Root"]];

Your plist file has a dictionary that contains an array. Try this

NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:filePath];
NSArray *array = [[NSArray alloc]initWithArray:[dict objectForKey:@"Root"]];
司马昭之心 2024-12-31 18:59:34
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:@"name.plist"];
NSArray *plistData = [NSArray arrayWithContentsOfFile:finalPath];
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:@"name.plist"];
NSArray *plistData = [NSArray arrayWithContentsOfFile:finalPath];
满天都是小星星 2024-12-31 18:59:33

您可以尝试将其加载到 NSDictionary 中并询问其中的 [ objectForKey:@"Root"] 吗?

据我记得,属性列表是键/值映射,而您的数组只是这些值之一。

Could you try loading it into a NSDictionary and asking for the [ objectForKey:@"Root"] inside it ?

As far as I remember, property lists are key / value maps and your array is just one of those values.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文