NSArray找到对象或对象 - 最佳实践 [英] NSArray find object or objects - best practices

查看:418
本文介绍了NSArray找到对象或对象 - 最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解决方案:
我已将@ BlackRider的答案标记为正确,因为它是最通用的,特别是对于复杂的比较,但还有其他非常好的答案和评论。我会鼓励任何有相同或类似问题的人查看它们并评估针对您特定情况的最佳行动方案。

Solution: I have marked @BlackRider's answer as correct as it is the most versatile especially for complex comparisons however there are other very good answers and comments. I would encourage anyone with the same or similar question to review them and evaluate the best course of action for your specific situation.

在我的情况下,我实际上并没有使用BlackRider的解决方案在我的实现中我已经选择使用我自己的解决方案(参见下面的编辑#2)和@JoshCaswell的评论以及@ voromax建议的 indexesOfObjectsWithOptions:passingTest:,这是因为在这种情况下,我的比较非常简单。

In my situation, I am actually not using BlackRider's solution in my implementation. I have elected to use my own solution (see Edit #2 below) with help from @JoshCaswell's comments as well as @voromax's suggestion of indexesOfObjectsWithOptions:passingTest: due to the fact that my comparisons are very simple in this situation.

感谢所有回答并提供见解的人。

Thanks to everyone who answered and provided insight.

我正在寻找一种基于该对象的属性(在这种情况下是唯一标识符)从 NSArray 中检索对象的有效方法。在使用Linq的C#.NET中我会做类似的事情

I am looking for an efficient way to retrieve an object from an NSArray based on a property of that object (a unique identifier, in this case). In C#.NET using Linq I would do something like

MyObject obj = myList.Single(o => o.uuid == myUUID);

我也想知道是否有一种有效的方法来获得匹配非唯一的对象数组属性。同样,使用Linq它看起来像

I am also wondering if there is an efficient way to get an array of objects matching a non-unique property. Again, with Linq it would look like

List<MyObject> objs = myList.Where(o => o.flag == true).ToList();

当然我可以编写循环来执行此操作,但它们不可重复使用我怀疑他们的表现。

Of course I can write loops to do this but they would not be reusable and I'm suspicious of their performance.

查找具有唯一ID的对象:

Finding an object with a unique ID:

-(MyObject*)findObjectWithUUID:(NSString*)searchUUID{
    for (MyObject* obj in _myArray){
        if([obj.uuid isEqualToString: searchUUID])
            return obj;
    }
}

查找对象数组:

-(NSArray*)findObjectsWithFlag:(BOOL)f{
    NSMutableArray* arr = [NSMutableArray array];
    for (MyObject* obj in _myArray){
        if(obj.flag == f)
            [arr addObject:obj];
    }
    return arr;
}

- 编辑

幸运的是,在第一种情况下,我要找的对象有一个唯一的标识符,我知道只有一个。我想出了一个在我的对象上实现isEqual的解决方案,它将由 indexOfObject调用:

Luckily in the first situation the object I am looking for has a unique identifier and I know there will only be one. I came up with a solution to implement isEqual on my object which will be invoked by indexOfObject:

- (BOOL)isEqual:(id)object{
    return [self.uuid isEqualToString: ((MyObject*)object).uuid];
}

然后创建一个假的查找对象并用它来查找真实的一个

And then create a "fake" lookup object and use that to find the real one

MyObject *lookupObject = [[MyObject alloc] init];
lookupObject.uuid = searchUUID;
MyObject *actualObject = 
    [_myArray objectAtIndex:[_myArray indexOfObject:lookupObject]];

这与我上面发布的for-in循环基本相同,但可能更具可读性和放大倍数;更可重复使用。当然,这只适用于找到一个独特的对象,而不是解决我问题的后半部分。

This is essentially the same as the for-in loop I posted above, but might be more readable & be more reusable. Of course, this only works for finding one unique object and does not address the second half of my question.

- 编辑2 -

按照评论中的建议检查 Class 并实现 hash

Checking Class and implementing hash as recommended in comments.

- (BOOL)isEqual:(id)object{
    return [object isKindOfClass:[MyObject class]] && 
           [self.uuid isEqualToString: ((MyObject*)object).uuid];
}

- (NSUInteger)hash{
    return [self.uuid hash];
}


推荐答案

您可以使用 [NSPredicate] ,它为搜索提供类似查询的语法。有关谓词语法说明,请查看此页面。这是一个简单的例子:

You can use [NSPredicate], which gives you a query-like syntax for search. Check out this page for the predicate syntax description. Here's a simple example:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"propertyName == %@", @"value"];
NSArray *filteredArray = [myArray filteredArrayUsingPredicate:predicate];

关于性能,我认为您的解决方案没问题,因为阵列中的任何搜索都需要遍历所有无论如何,然后,对于每个对象,将字段的值与您搜索的值进行比较。您可以在同一数据中优化重复搜索,例如通过创建和填充将某些字段的值映射到匹配对象(或对象集合,如果映射是一对多)的字典。

As to performance, I think your solution is OK since any search in an array needs to iterate through all the elements anyway, and then, for each object, compare the value of a field against the value you search for. You can optimize repeat searches within the same data, e.g. by creating and populating a dictionary that maps values of some field to the matching objects (or collections of objects, if the mapping is one to many).

这篇关于NSArray找到对象或对象 - 最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆