0

If I am given an object that I know is in an NSMutableArray, how can I access that NSMutableArray and the index of the object in that NSMutableArray?

1
  • int idx = [array indexOfObject:theObject]; Commented Jan 6, 2013 at 16:01

2 Answers 2

2

Not directly. You would, at least, need references to all possible arrays the object might be in. With that, you could ask each for objectAtIndex:. If the return value is not NSNotFound, the object is in the array.

Of course, that is potentially slow and a sign of bad design.

One solution would be to have a reference from your object to the array. This will work, but you need to makes sure the circular reference doesn't cause a leak.

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

Comments

0

You can use the function indexOfObject:(id)object on the array and pass your object as the argument to receive the index.

For instance, let your array be NSMutableArray *array and the object be id obj. Then, int index = [array indexObObject:obj];

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.