IndexOfObject返回2147483647

时间:2012-06-07 10:22:35

标签: iphone nsarray nsrangeexception

我有一个包含10个项目的数组。当我为元素编号9和元素编号10调用“IndexOfObject”时,Xcode会返回异常:“NSRangeException

  

原因:'_ [_ NSCFArray objectAtIndex:]索引: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);

3 个答案:

答案 0 :(得分:46)

当对象不在数组中时,

indexOfObject:返回NSNotFoundNSNotFound定义为NSIntegerMax(在iOS 32位上== 2147483647)。

所以看起来你正在寻找的对象就不存在了。

答案 1 :(得分:0)

     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.

答案 2 :(得分:-1)

默认情况下,假定整数已签名。

换句话说,编译器假定将调用整数变量来存储负数或正数。这限制了范围可以在任一方向上达到的程度。

例如,32位int的范围为4,294,967,295。实际上,因为值可能是正数或负数,所以范围实际为−2,147,483,648+2,147,483,647

如果我们知道永远不会要求变量存储负值,我们可以将其声明为无符号,从而将(正)范围扩展到0+4,294,967,295

unsigned int的指定如下:

unsigned int myint;