Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find indexOfObject from NSArray

I have an NSArray filled with custom objects. Each object has several variables: pk, amount, date etc..

I want to fetch the object that has the highest number in the pk variable. I can do this using:

 NSUInteger maximumpk = [[bets valueForKeyPath:@"@max.pk"] intValue];

This gives me the actual value from the highest pk. Now I need to get the index for that object. I have seen indexOfObject used when the array has just 1 variable of data, but how do I use it in this instance?

Thanks

like image 848
Darren Avatar asked Dec 21 '22 05:12

Darren


1 Answers

Use -indexOfObjectPassingTest:, for example:

NSUInteger idx = [bets indexOfObjectPassingTest:^(id obj, NSUInteger idx, BOOL *stop) {
    return [obj pk] == maximumpk;
}];
like image 107
一二三 Avatar answered Jan 05 '23 01:01

一二三