Domanda

How can I get the index of the highest (numerical) index contained in an NSMutableIndexSet?

I.e the position in which the highest NSUInteger (index) is in the 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];   

}
È stato utile?

Soluzione

Keep track as you add values to the index set:

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];

Altri suggerimenti

NSMutableIndexSet is a subclass of NSIndexSet. NSIndexSet understands the lastIndex selector:

Returns either the last index in the index set or the not-found indicator.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top