views:

40

answers:

1

Hello!

I have an array of strings.

How might I be able to figure out what index a string is in a array?

Thanks, Christian Stewart

+4  A: 

-[NSArray indexOfObject:]

Dave DeLong
wow? That was... Simple. Hahah I'll give it a try and get back to you.
Christian Stewart
This is only going to work if you have a reference to the same string object that was put in the array. Not just a string that has the same value.
Jerry Jones
It will work when using `indexOfObject:@"Some string"`! `indexOfObject:` uses `isEqual:` on the objects in the array, and `NSString's` implementation of `isEqual:` tests the value of the string, not the pointer ;)
Douwe Maan
I'm getting an error about the index being out of range when I try to use the value returned from indexOfObject: It says that the returned index is something crasy like 201341023 or something... My array is literally 3 things long.
Christian Stewart
That number is actually `NSNotFound`, which is returned when the object was not found in the array.
Douwe Maan
WOOT! Got it working because of a dumb error i fixed. Thanks so much douwe!
Christian Stewart
> indexOfObject: uses isEqual:Hmm, good info. I actually did not know that!
Jerry Jones