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

标签 ios nsarray plist nsdictionary

这就是原始 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。我希望它返回字典中的项目数量。

最佳答案

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

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

关于ios - 计算 .plist 字典项数(iOS SDK),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1403208/

相关文章:

iphone - iOS : How to attach a multiple attachment file in one button using pickerview method?

ios - 在 iOS 中的字典中使用字典数组的 NSPredicate 进行过滤

ios - NSMutableArray Init 的麻烦

ios - 基于单个对象键对 NSDictionary 进行排序

iphone - plist 值丢失

ios - 使用 UIButton 数组检测哪个按钮被按下

ios - 我如何在 swift ios 的导航栏标题中使用图像

macos - 启动后如何重启docker-machine?

ios - 通过shell脚本plist buddy从plist读取数组

ios - -[CALayer setFrame :] crash