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?
2 Answers
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.
int idx = [array indexOfObject:theObject];