Cocoa:不构建字典的属性列表的元素计数?
计算属性列表中条目数量的最佳方法是什么?
我目前从 plist 条目 (*) 构建一个字典,然后使用字典的计数:
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]);
这让我觉得不必要的“沉重”,但我无法找到更有效的解决方案。有什么想法吗?
(*) targeting 10.5 and therefore using the deprecated
+propertyListFromData:…
class method.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.如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧...如果您无论如何都要转换为 XML,您可以使用 NSXMLNode 的
childCount
方法。文档确实表明它比调用 [children count] 更有效,但是 NSXMLNode 的创建可能会使这与 NSDictionary 方法一样糟糕(甚至更糟)。你有简介吗?您正在处理特别大的 plist 吗?您是否经常请求此计数?我说:使用 NSDictionary,如果您经常请求它,则缓存该值,然后继续,除非速度慢得令人无法接受。 (是的,现在看起来很难看,但还有更重要的事情需要担心。)
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.)