68

i am trying to get index of an array through indexOfObject method as follows but when i try to log the value to test the index i get a garbage value.. for testing purposes i am having an array with values {57,56,58..} to get an index of lets say 56,

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

the value i get is something like 2323421. what am i possibly doing wrong??

3
  • Are you actually inserting it into the array? Commented Sep 13, 2011 at 7:24
  • What kind of data type value your myArray contains Commented Sep 13, 2011 at 7:24
  • i was adding a NSString value to array and comparing the array object with a NSNumber.. didnt thought it would be a problem but eventually had to compare the object with a string value to get it right.. Commented Sep 17, 2011 at 6:42

7 Answers 7

145

The index returned by indexOfObject will be the first index for an occurence of your object. Equality is tested using isEqual method.
The garbage value you get is probably equal to NSNotFound.
Try testing anIndex against it. The number you are looking for isn't probably in your array :

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

or log the content of the array to be sure :

NSLog(@"%@", myArray);
Sign up to request clarification or add additional context in comments.

2 Comments

i was adding a NSString value to array and comparing the array object with a NSNumber.. didnt thought it would be a problem but eventually had to compare the object with a string value to get it right..
What if i have same object multiple time?
40

Folks,

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

However, on IOS - because the indexOfObject returns an UNSIGNED int (aka NSUInteger) the returned index must be greater than or equal to zero. Since 'zero' is a valid index there is no way to indicate to the caller that the object was not found -- except by returning an agreed upon constant value that we all can test upon. This constant agreed upon value is called NSNotFound.

The 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 on the platform).

1 Comment

@joe If you CMD+click NSNotFound, you'll see that it is defined as enum {NSNotFound = NSIntegerMax};. I know both NSIntegerMax and NSUIntegerMax are both positive, but wouldn't it have been more sensible to use NSUIntegerMax since it used in functions that return NSUInteger? Also, what if I'm searching for an object that is placed at index NSIntegerMax of the array? Wouldn't I be tricked into thinking that the object is NSNotFound?
6
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);

Its giving the correct output, may be u have done something wrong with storing objects in ur array.

Comments

5

Try this:

NSArray's indexOfObject: method. Such as the following:

NSUInteger fooIndex = [someArray indexOfObject: someObject];

Comments

1

If you're using Swift and optionals make sure they are unwrapped. You cannot search the index of objects that are optionals.

Comments

0

I just checked. Its working fine for me. Check if your array has the particular number. It will return such garbage values if element is not present.

Comments

0

indexOfObject methord will get the index of the corresponding string in that array if the string is like @"Test" and you find like @"TEST" Now this will retun an index like a long number

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.