I have a NSArray, and I want to find the last occurrence of an element. For example:
[apple, oranges, pears, apple, bananas];
int i = lastIndexOf("apple");
out: i == 3;
I'm struggling to find a simple solution looking an the APIS, but there aren't example so it's pretty hard to understand which function I should use.
NSUInteger index = [array indexOfObjectWithOptions:NSEnumerationReverse
passingTest:^(id obj, NSUInteger i, BOOL *stop) {
return [@"apples" isEqualToString:obj];
}];
If the array doesn't contain @"apples"
, index
will be NSNotFound
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With