我来自 JavaScript/ActionScript 背景,习惯于处理字符串indexOf
和lastIndexOf
数组。对于NSString
s,这两个都比较简单:
// equivalent of JavaScript's string.indexOf(substring, fromIndex)
[string rangeOfString:substring
options:nil
range:NSMakeRange(fromIndex, string.length - fromIndex)].location;
// equivalent of JavaScript's string.lastIndexOf(substring, fromIndex)
[string rangeOfString:substring
options:NSBackwardsSearch
range:NSMakeRange(string.length - fromIndex, fromIndex)].location;
对于NSArray
s,我设法弄清楚indexOf
,但找不到要执行的本机函数lastIndexOf
:
// equivalent of JavaScript's array.indexOf(item, fromIndex)
[array indexOfObject:item
inRange:NSMakeRange(fromIndex, array.count - fromIndex)];
lastIndexOf
在范围内的数组中查找元素的代码是什么?它需要多个选择器调用吗?