如何删除具有相同的属性值,但在NSMutableArray中的一个对象

我有一个URLstring属性和标题的历史对象。 我想search包含searchstring的url的所有历史logging,然后删除所有重复项。

例如:我有一组历史对象,其中20个都是“ https://www.google.com ”,4个是“ https://www.google.com/#q=search ”,我想要只有一个对象的url值为“ https://www.google.com ”,一个url值为“ https://www.google.com/#q=search ”的数组返回

这是我当前的代码,它search历史logging并返回匹配string的所有对象:

- (NSArray *)historyObjectsContainingQuery:(NSString *)query { NSMutableArray *matchingObjects = [[NSMutableArray alloc] init]; HistoryManager *HM = [HistoryManager sharedInstance]; for (int i=0; i<[[HM history] count]; i++) { //History "days" HistoryObject *historyItem = HistoryObject([[HistoryManager sharedInstance] history][i]); for (int o=0; o<[historyItem.objects count]; o++) { //Each object inn each history day HistoryObject *HO = HistoryObject(historyItem.objects[o]); if ([HO.title rangeOfString:query options:NSCaseInsensitiveSearch].location != NSNotFound) { //history objects with their title containing the query string [matchingObjects addObject:HO]; } } } return matchingObjects; } 

然后我logging每个url值:

self.foundInBookmarksAndHistory = [self historyObjectsContainingQuery:textField.text]; (HistoryObject * HO in self.foundInBookmarksAndHistory){NSLog(@“%@”,HO.url); }

这里是结果:

 https://www.google.com/ 2013-09-15 13:48:49.382 Flow HD[3599:60b] https://www.google.com/ 2013-09-15 13:48:49.384 Flow HD[3599:60b] https://www.google.com/ 2013-09-15 13:48:49.385 Flow HD[3599:60b] https://www.google.com/ 2013-09-15 13:48:49.387 Flow HD[3599:60b] https://www.google.com/ 2013-09-15 13:48:49.388 Flow HD[3599:60b] https://www.google.com/ 2013-09-15 13:48:49.390 Flow HD[3599:60b] https://www.google.com/ 2013-09-15 13:48:49.391 Flow HD[3599:60b] https://www.google.com/search?q=yahoo 2013-09-15 13:48:49.392 Flow HD[3599:60b] https://www.google.com/ 2013-09-15 13:48:49.394 Flow HD[3599:60b] https://www.google.com/ 2013-09-15 13:48:49.395 Flow HD[3599:60b] https://www.google.com/search?q=Sup 2013-09-15 13:48:49.397 Flow HD[3599:60b] https://www.google.com/ 2013-09-15 13:48:49.398 Flow HD[3599:60b] https://www.google.com/ 2013-09-15 13:48:49.400 Flow HD[3599:60b] https://www.google.com/ 2013-09-15 13:48:49.402 Flow HD[3599:60b] https://www.google.com/ 2013-09-15 13:48:49.404 Flow HD[3599:60b] https://www.google.com/ 2013-09-15 13:48:49.405 Flow HD[3599:60b] https://www.google.com/ 

还有很多,我怎么能删除每个对象具有相同的值? 对象不相等,但是具有相同的URL。

我试过NSPredicate并使用更多的循环来通过并find匹配的对象,但即时通讯总是留下与2个或更多相同的对象具有相同的属性。

你可以做这样的事情:

 NSMutableSet *seenObjects = [NSMutableSet set]; NSPredicate *dupPred = [NSPredicate predicateWithBlock: ^BOOL(id obj, NSDictionary *bind) { HistoryObject *hObj = (HistoryObject*)obj; BOOL seen = [seenObjects containsObject:hObj.title]; if (!seen) { [seenObjects addObject:hObj.title]; } return !seen; }]; NSArray *yourHistoryArray = ... // This is your array which needs to be filtered NSArray *yourHistoryArray = [yourHistoryArray filteredArrayUsingPredicate:dupObjectsPred]; 

我不确定这是search所有对象的最佳方法,但除了我能想到的最简单的修复方法是replace

 if ([HO.title rangeOfString:query options:NSCaseInsensitiveSearch].location != NSNotFound) { [matchingObjects addObject:HO]; } 

有了这个:

 if ([HO.title rangeOfString:query options:NSCaseInsensitiveSearch].location != NSNotFound) { HistoryObject *temp = HistoryObject(historyItem.objects[o]); [historyItem.objects removeObjectAtIndex:o]; } //And after all the `for` loops [historyItem.objects addObject:temp]; 

您删除了所有匹配的对象,并将其添加到数组中一次。

编辑:阅读你的编辑后,使用isEqualToString而不是rangeOfStringrangeOfString会在abcd,abce和abcf中find单词abc,这不是你想要的。 abc的isEqualToString只对abc有效,没有别的。

如果对象仍然存在,则循环可变数组(如果需要,将数组更改为mutable)

 while ([farray containsObject:_data]){ [farray removeObject:_data]; }