Getting object index from NSArray?

I am trying to get the index of an array using the indexOfObject method as follows, but when I try to write a value to check the index, I get the garbage value. For testing purposes, I have an array with the values {57,56,58..} to get the let say 56 index,

 NSNumber *num = [NSNumber numberWithInteger:56]; NSInteger Aindex = [myArray indexOfObject:num]; NSLog(@" %d",Aindex); 

the value i get is similar to 2323421 . what can i do wrong?

+58
ios iphone nsarray
Sep 13 '11 at 7:16
source share
7 answers

The index returned by indexOfObject will be the first index for the occurrence of your object. Equality is checked using the isEqual method.
The selected garbage value is probably equal to NSNotFound .
Try testing anIndex against it. The amount you are looking for is probably not in your array:

 NSNumber *num=[NSNumber numberWithInteger:56]; NSInteger anIndex=[myArray indexOfObject:num]; if(NSNotFound == anIndex) { NSLog(@"not found"); } 

or write the contents of the array:

 NSLog(@"%@", myArray); 
+134
Sep 13 '11 at 7:25
source share

People,

When an object is not found in the array, indexOfObject does NOT return garbage. Many systems return an index of -1 if the item is not found.

However, in IOS - since indexOfObject returns UNSIGNED int (aka NSUInteger), the returned index must be greater than or equal to zero. Since null is a valid index, there is no way to tell the caller that the object was not found - except returning an agreed constant value that we can all check. This consistent consistent value is called NSNotFound.

Method:

 - (NSUInteger)indexOfObject:(id)anObject; 

will return NSNotFound if the object was not in the array. NSNotFound is a very large POSITIVE integer (usually 1 minus the maximum int value on the platform).

+38
Jan 26 '13 at 14:52
source share
 NSNumber *num1 = [NSNumber numberWithInt:56]; NSNumber *num2 = [NSNumber numberWithInt:57]; NSNumber *num3 = [NSNumber numberWithInt:58]; NSMutableArray *myArray = [NSMutableArray arrayWithObjects:num1,num2,num3,nil]; NSNumber *num=[NSNumber numberWithInteger:58]; NSInteger Aindex=[myArray indexOfObject:num]; NSLog(@" %d",Aindex); 

It gives the correct conclusion, maybe you did something wrong with saving the objects in the ur array.

+6
Sep 13 '11 at 7:26
source share

Try the following:

NSArray method indexOfObject indexOfObject: For example:

 NSUInteger fooIndex = [someArray indexOfObject: someObject]; 
+5
Mar 02 '16 at 13:34
source share

If you use Swift and options, make sure they are unpacked. You cannot search for an index of objects that are optional.

+1
Apr 24 '17 at 22:36
source share

I just checked. His work is wonderful for me. Check if your array has a specific number. It will return such garbage values ​​if the item is missing.

0
Sep 13 '11 at 7:26
source share

indexOfObject methord will get the index of the corresponding row in this array if the row is like @ "Test" and you find it as "TEST". Now it will reassign the index as a long number

0
Jun 02 '16 at 5:17
source share



All Articles