简体   繁体   English

计算plist数组中的项目

[英]count items in plist array

I am having difficult accessing the count of a chosen plist. 我很难访问所选plist的计数。 In the previous view controller, the user selects from one of several buttons, each one corresponding to a particular plist. 在上一个视图控制器中,用户从几个按钮之一中进行选择,每个按钮对应一个特定的plist。 That plist is sent to the current vc as NSString * chosenPlist. 该plist作为NSString * selectedPlist发送到当前的vc。 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? 我有NSLogged来查看documents目录和plistpath似乎正确,但是我要计数的数组仍然为0。如何计算所选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"];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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