3

URL 文字列プロパティとタイトルを持つ履歴オブジェクトがあります。検索文字列を含む URL を持つオブジェクトのすべての履歴を検索し、すべての重複を削除したいと考えています。

例: 履歴オブジェクトの配列があり、そのうち 20 個はすべて " https://www.google.com " で、4 個は " https://www.google.com/#q=search " です。取得したい" https://www.google.com " の URL 値と " https://www.google.com/#q=search "の 1 つの URL 値を持つ 1 つのオブジェクトのみを含む配列を返します

履歴を検索し、文字列に一致するすべてのオブジェクトを返す現在のコードは次のとおりです。

    - (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 値をログに記録します。

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 は同じです。

NSPredicate を試し、さらに for ループを使用して一致するオブジェクトを見つけましたが、同じプロパティを持つ同じオブジェクトが常に 2 つ以上残っています。

4

3 に答える 3

9

次のようなことができます:

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];
于 2013-09-15T21:05:03.080 に答える