Iphone IndexOfObject返回2147483647
我有一个包含10项的数组。当我为编号9和编号10的元素调用“Iphone IndexOfObject返回2147483647,iphone,nsarray,nsrangeexception,Iphone,Nsarray,Nsrangeexception,我有一个包含10项的数组。当我为编号9和编号10的元素调用“IndexOfObject”时,Xcode返回异常:nsrangefexception” 原因:'\u[\ n法拉利对象索引:]索引:2147483647 界限(10)’ 从前面的NSLog中,我看到数组中存在这两个元素,但indexOfObject没有找到它们。为什么? 我的代码是: NSDictionary * headConfig =[avatarDictionaryToSave objectForKey:@"head_d
IndexOfObject
”时,Xcode返回异常:nsrangefexception
”
原因:'\u[\ n法拉利对象索引:]索引: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:
当数组中不存在对象时,返回NSNotFound
NSNotFound
定义为NSIntegerMax
(==2147483647,在iOS 32位上)
因此,您正在查找的对象似乎不在那里。默认情况下,假定整数是有符号的 换句话说,编译器假定将调用整数变量来存储负数或正数。这限制了范围在任一方向上可以达到的范围 例如,32位int的范围为
4294967295
。实际上,由于该值可能为正或负,因此范围实际上是−2147483648
至+2147483647
如果我们知道变量永远不会被调用来存储负值,我们可以将其声明为无符号,从而将(正)范围扩展到0
到+4294967295
按如下方式指定无符号整数:
unsigned int myint;
如何检索索引?您似乎混淆了
indexOfObject
和objectAtIndex
?前者将查找对象,而后者在数组中的索引处检索对象?这并不能回答问题。或者在64位体系结构上,返回9223372036854775807。请注意,这也是一种情况的失败之一。我遇到过同样的情况,所以我在这里回答。如果您尝试了其他解决方案,但仍然无法找到解决方案,那么也可以尝试此解决方案。
Please change the coding of adding the array values as I mentioned below.
// [arr_values addObject:[dictionary valueForKey:@"Name"]];
[arr_values addObject:[NSString stringWithFormat:@"%@",[dictionary valueForKey:@"Name"]]];
When target array elements are not in string format then while we using the
indexOfObject then that value can't able to find in the target array. So please try to
change the value of object as mentioned above while adding into array.