【发布时间】:2012-06-07 10:22:34
【问题描述】:
我有一个包含 10 个项目的数组。当我为元素编号 9 和元素编号 10 调用“IndexOfObject”时,Xcode 返回异常:“NSRangeException”
原因:'_[_NSCFArray objectAtIndex:] index:2147483647 超出 边界(10)'。
从之前的NSLog 中,我看到这两个元素存在于数组中,但indexOfObject 没有找到它们。为什么?
我的代码是:
NSDictionary * headConfig =[avatarDictionaryToSave objectForKey:@"head_dictionary"];
NSString * headImage =[headConfig objectForKey:@"layer_key"];
NSString * pathFace =[[NSBundle mainBundle]pathForResource:@"Face" ofType:@"plist"];
NSLog(@"%@", headImage);
NSArray *arrayFace =[NSArray arrayWithContentsOfFile:pathFace];
NSLog(@"the elements are: %@", arrayFace);//here headImage is present
int index =[arrayFace indexOfObject:headImage];
NSLog(@"the index is %d", index);
【问题讨论】:
-
你是如何检索索引的?
-
您似乎混淆了
indexOfObject和objectAtIndex?前者会找到对象,而后者会在数组中的索引处检索对象?
标签: iphone nsarray nsrangeexception