I know indexOfObject could return the index of the element in an array, but I check this method is an NSArray method rather than an NSMutableArray method, and the element in my array is object, does that matter? Because currently my program crashes when the following code gets invoked.
Class *obj = [[Class alloc] init];
obj.textField1 = outlet1.text;
obj.textField2 = outlet2.text;
NSUInteger index = [myMutableArray indexOfObject:obj];
outlet1.text = [[myMutableArray objectAtIndex:index-1] textField1];
outlet2.text = [[myMutableArray objectAtIndex:index-1] textField2];
[obj release];
What I am trying to do here is when I call above code, those outlets show the details of the previous object in the array. HELP?
Ok, here is what I really want to do:
I want to create an object by entering stuff in two text fields, then store the object in an array. The array I’m talking about is already declared in the header file and allocated space. The above code is actually a method to show the previous object that’s already created. The first three lines are intended to make an object that represents the current one showing in the text fields. Then I want to look it up in my array and get the index of it. The reason I use “index-1” is because I want to see the previous object I created. And by “crash” I mean the program simply exits and goes back to the home screen on my simulator.
Thanks!
All right, I have finally worked out my solution to my problem, I found another way to get the current object, instead of “creating” an “new” object and compare it with ones in the array, I use a flag to record the index of the current object on the screen and decrement it to get the previous object. Anyway, thank you guys.
NSMutableArray extends NSArray and inherits all of its methods. So calling
objectAtIndex:on an NSMutableArray is fine.Without a crash log I can’t tell what the problem is, but my first guess would be that
index-1is-1whenindexis0, or out of bounds whenindexOfObject:returnsNSNotFound.