计算 .plist 字典项数(iOS SDK)

发布于 2024-08-04 06:36:18 字数 1033 浏览 6 评论 0原文

这就是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

漫雪独思 2024-08-11 06:36:18

您的 return 语句正在转发字典第一个元素中的项目数,而不是字典本身的项目数。相反,我认为你想要的是这样的:

return [NSString stringWithFormat:@"%d Accounts", [plistDict count]];

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:

return [NSString stringWithFormat:@"%d Accounts", [plistDict count]];
清浅ˋ旧时光 2024-08-11 06:36:18

这似乎是旧的 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 an NSArray, rather than an NSDictionary. If it's actually a dictionary, use a string value for the key, rather than 0.

烟雨扶苏 2024-08-11 06:36:18

[plistDict count] 怎么样?由于您将 0 作为字典的键传递,因此您将无法返回任何内容,因为您不能使用 nil 作为键或值。

What about [plistDict count]? Since you're passing in 0 as the key to the dictionary you won't get anything back since you can't use nil as a key or a value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文