0

如何获取包含在 中的最高(数字)索引的索引NSMutableIndexSet

即最高 NSUInteger(索引)在NSMutableIndexSet

NSArray *results = [subCategory filteredArrayUsingPredicate:predicate];

if([results count] == 1) {

    returns = [results objectAtIndex:0];

 }

else if ([results count] > 1) {

     NSMutableIndexSet *set = [NSMutableIndexSet indexSet];

     for (NSString *subCat in results) {

        NSUInteger indexs = 0;

        for (NSString *comp in components) {

            if ([subCat rangeOfString:comp].location != NSNotFound) {

                indexs ++;

            }

        }

        [set addIndex:indexs];

    }


    // find the position of the highest amount of matches in the index set

    NSUInteger highestIndex = ...;

    returns = [results objectAtIndex:highestIndex];   

}
4

2 回答 2

0

NSMutableIndexSet是 的子类NSIndexSetNSIndexSet了解lastIndex选择器

返回索引集中的最后一个索引或未找到的指示符。

于 2012-10-29T00:40:36.993 回答
0

在向索引集添加值时跟踪:

NSMutableIndexSet *set = [NSMutableIndexSet indexSet];
NSUInteger maxIndexs = 0;
NSUInteger indexOfMax = NSNotFound;
for (NSString *subCat in results) {
    NSUInteger indexs = 0;
    for (NSString *comp in components) {
        if ([subCat rangeOfString:comp].location != NSNotFound) {
            indexs ++;
        }
    }

    if (indexes > maxIndexs) {
        maxIndexs = indexs;
        indexOfMax = set.count;
    }
    [set addIndex:indexs];
}

// find the position of the highest amount of matches in the index set
NSUInteger highestIndex = indexOfMax;

returns = [results objectAtIndex:highestIndex];
于 2012-10-29T00:42:08.193 回答