Get NSRange from NSArray



所以我有NSArray包含对象,如{117, 22}{http://t.co/7l3oiMDQt3},我想得到11722我试图使用ObjectAtIndex:,应用程序崩溃了,然后添加了这个:

if(array.count > 0) {                                           
   NSLog(@"%@",[array objectAtIndex:1]);
}

程序仍然崩溃

是否有其他方法可以从数组中获得NSRange的位置和长度?


编辑:

数组内的对象是NSDataDetector,它获得一个字符串的链接。

当它崩溃时它会这样说:

 *** Terminating app due to uncaught exception 'NSRangeException', reason: '*[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
***

您的检查仍然可能超出数组范围:

如果学习。计数> 0){
NSLog(@ % @",[数组,objectAtIndex: 0]);//不是1}

在不知道数组的确切长度的情况下,永远不要随意检查数组中的索引。当你处理一个长度变化的数组时,你应该这样做,以防止试图访问一个不存在的索引:

for (NSInteger index = 0; index < array.count; index++) {
    NSLog(@"%@", array[index]);
}

你永远不会得到一个越界异常。

最新更新