计算 .plist 字典项数(iOS SDK)
这就是 plist 的原始样子:
{
authorLastName = Doe;
authorFirstName = Jane;
imageFilePath = "NoImage.png";
title = "Account Test 1";
year = 2009;
},
{
authorLastName = Doe;
authorFirstName = John;
imageFilePath = "NoImage.png";
title = "Account Test 2";
year = 2009;
},
我想计算 plist 的总项目数,并将它们显示为:“4 个帐户”。
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *myPlistPath = [documentsDirectory
stringByAppendingPathComponent:@"Accounts.plist"];
NSDictionary *plistDict = [[NSDictionary alloc] initWithContentsOfFile:myPlistPath];
NSArray *array = [[plistDict objectForKey:0]
sortedArrayUsingSelector:@selector(compare:)];
return([NSString stringWithFormat:@"%d Accounts", [array count]]);
但是,结果返回 0。我希望它返回字典中的项目数量。
This is what the plist looks like raw:
{
authorLastName = Doe;
authorFirstName = Jane;
imageFilePath = "NoImage.png";
title = "Account Test 1";
year = 2009;
},
{
authorLastName = Doe;
authorFirstName = John;
imageFilePath = "NoImage.png";
title = "Account Test 2";
year = 2009;
},
I want to count the total items of a plist, and display them like: '4 Accounts'.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *myPlistPath = [documentsDirectory
stringByAppendingPathComponent:@"Accounts.plist"];
NSDictionary *plistDict = [[NSDictionary alloc] initWithContentsOfFile:myPlistPath];
NSArray *array = [[plistDict objectForKey:0]
sortedArrayUsingSelector:@selector(compare:)];
return([NSString stringWithFormat:@"%d Accounts", [array count]]);
However, the result returns 0. I want it to return the ammount of items in the dictionary.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的 return 语句正在转发字典第一个元素中的项目数,而不是字典本身的项目数。相反,我认为你想要的是这样的:
Your return statement is relaying the number of items in the first element of the dictionary, not the number of items of the dictionary itself. Instead what I think what you want is something like:
这似乎是旧的 OpenStep 属性列表,支持只读格式。
您的代码看起来不错,只是
NSDictionary
仅将对象作为键,因此“[plistDict objectForKey:0]
”不会执行您期望的操作。如果您知道属性列表的顶层是一个数组,请将其分配给NSArray
,而不是NSDictionary
。如果它实际上是一个字典,请使用字符串值作为键,而不是 0。That appears to be an old OpenStep property list, which is supported as a read-only format.
Your code looks okay, except that
NSDictionary
only takes objects as keys, so "[plistDict objectForKey:0]
" is not going to do what you expect. If you know the top level of your property list is an array, then assign it to anNSArray
, rather than anNSDictionary
. If it's actually a dictionary, use a string value for the key, rather than 0.[plistDict count]
怎么样?由于您将0
作为字典的键传递,因此您将无法返回任何内容,因为您不能使用nil
作为键或值。What about
[plistDict count]
? Since you're passing in0
as the key to the dictionary you won't get anything back since you can't usenil
as a key or a value.