12

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);
Ortensia C.
  • 4,666
  • 11
  • 43
  • 70

3 Answers3

45

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.

Nikolai Ruhe
  • 81,520
  • 17
  • 180
  • 200
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.
SURESH SANKE
  • 1,653
  • 17
  • 34
  • For your information this is also one of the failure for one case. I have faced the same case so I have answered here. If you try some other solutions still not able to find solution then try this too. – SURESH SANKE Nov 08 '18 at 20:47
-1

By default, an integer is assumed to be signed.

In other words the compiler assumes that an integer variable will be called upon to store either a negative or positive number. This limits the extent that the range can reach in either direction.

For example, a 32-bit int has a range of 4,294,967,295. In practice, because the value could be positive or negative the range is actually −2,147,483,648 to +2,147,483,647.

If we know that a variable will never be called upon to store a negative value, we can declare it as unsigned, thereby extending the (positive) range to 0 to +4,294,967,295.

An unsigned int is specified as follows:

unsigned int myint;
sjas
  • 18,644
  • 14
  • 87
  • 92
Jonathan Gurebo
  • 169
  • 2
  • 3
  • 9