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.
That appears to be an old OpenStep property list, which is supported as a read-only format.
Your code looks okay, except that
NSDictionaryonly 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.