views:

32

answers:

1

What is the best way to count the number of entries in a property list?

I currently build a dictionary from the plist entries (*) and then use the dictionary's count:

NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:myPlistPath];
NSDictionary *myPlistDict = (NSDictionary *) [NSPropertyListSerialization
                    propertyListFromData:plistXML
                        mutabilityOption:NSPropertyListMutableContainersAndLeaves
                                  format:&format
                        errorDescription:&errorDesc];
NSLog(@"There are %d entries in the plist.", [myPlistDict count]);

This strikes me as unnecessarily "heavy", but I was not able to find a more efficient solution. Any ideas?



(*) targeting 10.5 and therefore using the deprecated +propertyListFromData:… class method.

+1  A: 

Well... if you're converting to XML anyway, you could use NSXMLNode's childCount method. The documentation does suggest that it's more efficient than calling [children count], but the creation of the NSXMLNode might make this just as bad (or even worse than) the NSDictionary method.

Have you profiled? Are you working with particularly large plists? Are you requesting this count often? I say: use NSDictionary, cache the value if you request it often, and move on unless this is unacceptably slow. (Yeah, it looks ugly right now, but there are bigger things to worry about.)

andyvn22
Thanks for the answer. I don't need to calculate this often, so I did what you suggested and moved on… I do find it strange though that there should be no easier way of doing it.
Yang