Objective-c:找到NSArray中元素的最后一个索引 [英] Objective-c: find the last index of an element in a NSArray
本文介绍了Objective-c:找到NSArray中元素的最后一个索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个NSArray,我想查找元素的最后一个出现。
例如:
[apple,oranges,pear,apple,bananas];
int i = lastIndexOf(apple);
out:i == 3;
我正在努力寻找一个简单的解决方案寻找一个APIS,但没有这样的例子
解决方案
NSUInteger index = [array indexOfObjectWithOptions:NSEnumerationReverse
passingTest:^(id obj,NSUInteger i,BOOL * stop){
return [@applesisEqualToString:obj];
}];
如果数组不包含 @apples
, index
将会是 NSNotFound
。
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
.
这篇关于Objective-c:找到NSArray中元素的最后一个索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文