1
self.listData = [NSArray arrayWithObjects:@"A",@"B",@"C", nil]; 

How can I get index numbers for listData items?

This code below is used to get listData items string:

c = [NSString stringWithFormat:@"%@",[listData objectAtIndex:indexPath.row]; 

Any idea? Thanks

2 Answers 2

4

Something like...

NSUInteger index = [listData indexOfObject:@"A"];

From the docs:

Return Value The lowest index whose corresponding array value is equal to anObject. If none of the objects in the array is equal to anObject, returns NSNotFound.

Discussion Objects are considered equal if isEqual: returns YES.

** Important: If anObject is nil an exception is raised.**

Sign up to request clarification or add additional context in comments.

Comments

0

If you can guarantee that the strings in your array are unique, then using indexOfObject: will work. Otherwise, you'll need to use one of the indexesOfObjects* methods such as indexesOfObjectsWithOptions:passingTest: to get a set of all of the indexes containing the object you're interested in.

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.