检查 NSArray 中 NSDictionary 键是否存在的更好选择?

发布于 2024-08-21 10:24:47 字数 368 浏览 9 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

忘羡 2024-08-28 10:24:47

是的。使用“快速枚举”,通常也称为 for-in 循环:

for (NSDictionary* dict in myArray) {

此外,要比较 NSString,请使用 -isEqualToString:

   if ([[dict objectForKey: myKey] isEqualToString:myString]) {

也就是说,没有对此进行算法改进(即该方法已经是最好的。)

Yes. Use "fast enumeration", commonly also known as for-in loop:

for (NSDictionary* dict in myArray) {

Also, to compare NSString's, use -isEqualToString:.

   if ([[dict objectForKey: myKey] isEqualToString:myString]) {

That said, there is no algorithmic improvement to this (i.e. this method is already the best.)

云归处 2024-08-28 10:24:47

这是您使用当前数据结构所能达到的最快速度。您正在对数组中的每个字典进行 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 with NSStrings. 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.

美煞众生 2024-08-28 10:24:47

您应该使用快速枚举,它将在幕后使用 C 数组迭代对象。现在,您每次循环时都会调用 -objectAtIndex: 和 -count 。

如果 myKey 是字符串,您还可以检查 NSPredicate。我的直觉告诉我它会更慢,但你永远不知道它是否会受益于 NSDictionary 的内部优化:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%@ LIKE %@", myKey, myString];
BOOL found = ([[myArray filteredArrayUsingPredicate:predicate] count] > 0);

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:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%@ LIKE %@", myKey, myString];
BOOL found = ([[myArray filteredArrayUsingPredicate:predicate] count] > 0);
鹿港小镇 2024-08-28 10:24:47

您可以对键值编码更加简洁:

[[myArray valueForKey:myKey] containsObject:myString];

它不一定更快(我怀疑它会更慢),但速度并不总是主要考虑的问题。在特定情况下速度是否至关重要是由分析决定的。

You could be much more succinct with Key-Value Coding:

[[myArray valueForKey:myKey] containsObject:myString];

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.

薔薇婲 2024-08-28 10:24:47

使用 == 检查字符串相等性可能会导致意外行为,因为您实际上是在比较指针(如果您确定正在处理两个对象所指向的单个字符串对象,那么这可能没问题)指针)。 isEqualToString: 可能是您想要的。

您可以使用“快速枚举”来稍微简化事情:

bool found = NO;

for (NSDictionary *dict in myArray) {
    found = [[dict objectForKey:myKey] isEqualToString:myString];

    if (found)
        break;
}

它只是“更快”,因为它需要编写的单词更少;执行速度是一样的。

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:

bool found = NO;

for (NSDictionary *dict in myArray) {
    found = [[dict objectForKey:myKey] isEqualToString:myString];

    if (found)
        break;
}

It's only "faster" in the sense that it's fewer words to write; the execution speed is the same.

你如我软肋 2024-08-28 10:24:47

收到您的对象后,您可以检查您收到的对象是“NSArray”或“NSDictionary”或“NSString”等。您可以使用以下代码来验证您的对象。

if([obj isKindOfClass:[NSArray class]]){
    NSLog(@"IS NSArray");
}
else if([obj isKindOfClass:[NSDictionary class]]){
    NSLog(@"Is NSDictionary");
}
else
{
    NSLog(@"Other");
}

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.

if([obj isKindOfClass:[NSArray class]]){
    NSLog(@"IS NSArray");
}
else if([obj isKindOfClass:[NSDictionary class]]){
    NSLog(@"Is NSDictionary");
}
else
{
    NSLog(@"Other");
}
疧_╮線 2024-08-28 10:24:47

通过快速枚举

BOOL found;

for (NSDictionary *dict in array) {

        if ([[dict objectForKey:@"YOURKEY"] isEqualToString:@"YOURVALUE"]) {
            found = YES;
            break;
        } else {
            found = NO;
        }
    }

With fast enumeration

BOOL found;

for (NSDictionary *dict in array) {

        if ([[dict objectForKey:@"YOURKEY"] isEqualToString:@"YOURVALUE"]) {
            found = YES;
            break;
        } else {
            found = NO;
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文