4

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

1
  • Hi Darren, You may need to override the - (BOOL) isEqual(NSObject*) in your custom class. When you call indexOfObject in a NSArray it calls the isEqualMethod. So you can check the equality of the self.pk to the passing objects pk value. Commented Dec 10, 2011 at 12:01

1 Answer 1

11

Use -indexOfObjectPassingTest:, for example:

NSUInteger idx = [bets indexOfObjectPassingTest:^(id obj, NSUInteger idx, BOOL *stop) {
    return [obj pk] == maximumpk;
}];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.