检查 NSArray 中 NSDictionary 键是否存在的更好选择?
我有一个 NSDictionaries 的 NSArray。我需要检查 NSArray 中 NSDictionary 的键是否至少出现一次对象。我通过使用来做到这一点
int i;
for (i=0;i< [myArray count];i++)
{
if ([[[myArray objectAtIndex: i] objectForKey: myKey] isEqualToString: myString]) {
found = YES;
break;
} else {
found = NO;
}
}
,但我怀疑有更好/更快的替代方案......
谢谢
I have an NSArray of NSDictionaries. I need to check if there's at least one occurrence of an object for a key of the NSDictionary in the NSArray. I do this by using
int i;
for (i=0;i< [myArray count];i++)
{
if ([[[myArray objectAtIndex: i] objectForKey: myKey] isEqualToString: myString]) {
found = YES;
break;
} else {
found = NO;
}
}
But I have a suspicion that there's a better/faster alternative for it...
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
是的。使用“快速枚举”,通常也称为
for-in
循环:此外,要比较
NSString
,请使用-isEqualToString:
。也就是说,没有对此进行算法改进(即该方法已经是最好的。)
Yes. Use "fast enumeration", commonly also known as
for-in
loop:Also, to compare
NSString
's, use-isEqualToString:
.That said, there is no algorithmic improvement to this (i.e. this method is already the best.)
这是您使用当前数据结构所能达到的最快速度。您正在对数组中的每个字典进行 O(1) 查找。如果您有大量字典,这可能会变得昂贵,因此您可以考虑(很大程度上取决于数据的语义)保留一个单独的后备 NSSet,其中包含包含字典中所有值的字符串对象集。然后您可以在该集合中检查一次是否存在。
请告诉我们有关数据形式的更多信息,以获得更多见解...
还要小心使用
NSString
的==
运算符。如果您实际上要检查字符串的文本是否相等,则应该使用-isEqualToString:
来代替,因为您的表单只会进行引用比较。This is as fast as you can get it with your current data structures. You're doing an O(1) lookup for each dictionary in the array. If you have a huge number of dictionaries, this might get expensive, so you could consider (depending very much on the semantics of your data) keeping a separate lookaside NSSet that contains the set of string objects comprising all the values in the dictionaries. Then you can check once in that set for existence.
Tell us more about the form of the data for more insight...
Also be careful with the
==
operator withNSString
s. If you're actually checking to see whether the text of the string is equal, you should use-isEqualToString:
instead, as your form will just do a reference comparison.您应该使用快速枚举,它将在幕后使用 C 数组迭代对象。现在,您每次循环时都会调用 -objectAtIndex: 和 -count 。
如果 myKey 是字符串,您还可以检查 NSPredicate。我的直觉告诉我它会更慢,但你永远不知道它是否会受益于 NSDictionary 的内部优化:
You should use fast enumeration, which will iterate through the objects using a C array behind the scenes. Right now, you're calling both -objectAtIndex: and -count each time through the loop.
You might also check out NSPredicate if myKey is a string. My gut tells me it would be slower, but you never know if it might benefit from internal optimization for NSDictionary:
您可以对键值编码更加简洁:
它不一定更快(我怀疑它会更慢),但速度并不总是主要考虑的问题。在特定情况下速度是否至关重要是由分析决定的。
You could be much more succinct with Key-Value Coding:
It's not necessarily faster (I suspect it will be slower), but speed is not always the primary concern. Whether speed is critical in a particular case is a matter for profiling to decide.
使用
==
检查字符串相等性可能会导致意外行为,因为您实际上是在比较指针(如果您确定正在处理两个对象所指向的单个字符串对象,那么这可能没问题)指针)。isEqualToString:
可能是您想要的。您可以使用“快速枚举”来稍微简化事情:
它只是“更快”,因为它需要编写的单词更少;执行速度是一样的。
Using
==
to check string equality might cause unexpected behavior, because you're actually comparing pointers (that can be okay if you're sure that you're dealing with a single string object pointed to by two pointers).isEqualToString:
is probably what you want instead.You can use "fast enumeration" to simplify things slightly:
It's only "faster" in the sense that it's fewer words to write; the execution speed is the same.
收到您的对象后,您可以检查您收到的对象是“NSArray”或“NSDictionary”或“NSString”等。您可以使用以下代码来验证您的对象。
After receiving your object, You can check that your received object is "NSArray" or "NSDictionary" or "NSString" etc. You can use following code to verify your object.
通过快速枚举
With fast enumeration