NSMutableIndexSet中最高索引的索引

时间:2012-10-29 00:36:15

标签: objective-c ios set indexing nsindexset

如何获取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];   

}

2 个答案:

答案 0 :(得分:0)

NSMutableIndexSetNSIndexSet的子类。 NSIndexSet了解lastIndex selector

  

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

答案 1 :(得分: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];