count items in plist array

流过昼夜 提交于 2019-12-13 05:37:53

问题


I am having difficult accessing the count of a chosen plist. In the previous view controller, the user selects from one of several buttons, each one corresponding to a particular plist. That plist is sent to the current vc as NSString * chosenPlist. I have NSLogged to see that the documents directory and the plistpath seem to be correct, but the array that I want to count is still 0. How can I count the number of items in the chosen plist?

NSString * chosenPlistPlus = [chosenPlist stringByAppendingString:@".plist"];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSLog(@"The documentsdirectory is %@", documentsDirectory);

NSString *myPlistPath = [documentsDirectory stringByAppendingPathComponent:chosenPlistPlus];
NSLog(@"MyPlistPath = %@", myPlistPath);



NSArray *arr = [NSArray arrayWithContentsOfFile: myPlistPath];
//Trying to count the items in the plist, which is an array of dicts.
    int count = arr.count;

NSLog(@"Count is %i", count);

here is a log:

2013-11-16 21:13:07.037 GlobalHistoryRegents[70407:70b] The documentsdirectory is /Users/username/Library/Application Support/iPhone Simulator/7.0.3/Applications/896EF347-A35E-40E2-9BE1-8CFAC5303347/Documents
2013-11-16 21:13:07.037 GlobalHistoryRegents[70407:70b] MyPlistPath = /Users/username/Library/Application Support/iPhone Simulator/7.0.3/Applications/896EF347-A35E-40E2-9BE1-8CFAC5303347/Documents/methodologyQuestions.plist
2013-11-16 21:13:07.038 GlobalHistoryRegents[70407:70b] Count is 0

回答1:


try using,

NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:myPlistPath];
NSArray *arr =dictionary[@"Root"];


来源:https://stackoverflow.com/questions/20026388/count-items-in-plist-array

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!