简体   繁体   中英

count items in plist array

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

try using,

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM