无法将plist加载到NSArray中

时间:2012-01-03 19:19:31

标签: objective-c nsarray plist

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

enter image description here

以下是我用来加载数据的代码:

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

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

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:1)

NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:@"name.plist"];
NSArray *plistData = [NSArray arrayWithContentsOfFile:finalPath];