如何删除所有具有相同属性值但在 NSMutableArray 中的对象 [英] How to remove all objects with the same property value but one in NSMutableArray
问题描述
我有一个带有 url 字符串属性和标题的历史对象.我想使用包含搜索字符串的 url 搜索对象的所有历史记录,然后删除所有重复项.
I have a history object with a url string property and title. I want to search all the history for objects with the url containing a search string, then remove all duplicates.
示例:我有一系列历史对象,其中 20 个都是https://www.google.com"和4 是https://www.google.com/#q=search",我想要返回一个只有一个对象的数组,url 值为 "https://www.google.com" 和一个 url 值 "https://www.google.com/#q=search"
Example: I have an array of history objects and 20 of them are all "https://www.google.com" and 4 are "https://www.google.com/#q=search", I want to get an array back with only one object with the url value of "https://www.google.com" and one url value of "https://www.google.com/#q=search"
这是我当前的代码,用于搜索历史记录并返回与字符串匹配的所有对象:
Here is my current code that searches the history and returns all objects matching the 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;
}
然后我记录每个 url 值:
Then i log each url value:
self.foundInBookmarksAndHistory = [self historyObjectsContainingQuery:textField.text];for (HistoryObject *HO in self.foundInBookmarksAndHistory) {NSLog(@"%@",HO.url);}
self.foundInBookmarksAndHistory = [self historyObjectsContainingQuery:textField.text]; for (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.
There is A LOT more, how can i remove each object with that same value? the objects are not equal, but do have equal urls.
我尝试过 NSPredicate 并使用更多的 for 循环来遍历并找到匹配的对象,但我总是留下 2 个或更多具有相同属性的相同对象.
I've tried NSPredicate and using more for loops to go through and find matching objects but im always left with 2 or more of the same objects with the same property.
推荐答案
你可以这样做:
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];
这篇关于如何删除所有具有相同属性值但在 NSMutableArray 中的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!