Search code examples
iphonensarraynsrangeexception

IndexOfObject return 2147483647


I have an array with 10 items. When I call "IndexOfObject" for the elements number 9 and the element number 10 Xcode return an exception: "NSRangeException"

reason: '_[_NSCFArray objectAtIndex:] index:2147483647 beyond bounds(10)'.

From a previous NSLog, I saw that the two elements exist in the array but indexOfObject not find them. Why?

My code is:

    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);

Solution

  • indexOfObject: returns NSNotFound when the object is not present in the array. NSNotFound is defined as NSIntegerMax (== 2147483647 on iOS 32 bit).

    So it seems that the object you are looking for is just not there.