本文介绍了从目标c中的数组中删除重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含自定义对象的数组.每个数组项都有一个名为名称"的字段.现在,我想根据此名称值删除重复的条目.

I have an array with custom objects. Each array item has a field named "name". Now I want to remove duplicate entries based on this name value.

我应该如何实现这一目标.

How should I go about achieving this.

提前谢谢.

推荐答案

您可能必须亲自编写此过滤方法:

You might have to actually write this filtering method yourself:

@interface NSArray (CustomFiltering)
@end

@implementation NSArray (CustomFiltering)

- (NSArray *) filterObjectsByKey:(NSString *) key {
   NSMutableSet *tempValues = [[NSMutableSet alloc] init];
   NSMutableArray *ret = [NSMutableArray array];
   for(id obj in self) {
       if(! [tempValues containsObject:[obj valueForKey:key]]) {
            [tempValues addObject:[obj valueForKey:key]];
            [ret addObject:obj];
       }
   }
   [tempValues release];
   return ret;
}

@end

这篇关于从目标c中的数组中删除重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 08:24
查看更多