How to get index in an mutable array?
I know indexOfObject
could return th开发者_C百科e 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-1
is -1
when index
is 0
, or out of bounds when indexOfObject:
returns NSNotFound
.
You're doing index - 1 which, if index == 0 then will be an invalid position. The function returns an INDEX. which is already 0 based. :)
Michael, to store the object in a mutable array...
YourObject *obj = [[YourObject alloc] init];
obj.textField1 = ... ;
obj.textField2 = ... ;
NSMutableArray *array = [NSMutableArray arrayWithCapacity:1];
[array addObject:obj];
Later, to get the object from the mutable array...
for (ID *obj in array)
{
if ([obj isKindOfClass:[YourObject class]])
{
outlet1.text = (SomeObject)obj.textField1;
outlet2.text = (SomeObject)obj.textField2;
}
}
Hope this helps.
First of all, when is obj
even put into the array in your above code? It seems as though you are trying to see if a Class
object is in your array, but you are allocating a new one, and then checking. You should use indexOfObject:
if you have an actual reference to the object in the array, otherwise, how could it be in there? Also, indexOfObject:
returns NSNotFound
if the object doesn't exist, which is probably why your method is crashing. If you want to find the earliest object of a certain class in your array, try:
for(int i = 0; i < [myMutableArray count]; i++){
if([[myMutableArray objectAtIndex:i] isKindOfClass:[Class class]]){
// Do stuff.
break;
}
}
Hope that helps!
This is a job for your debugger. Put a breakpoint in the line that crashes, and check the values of your variables. It sounds like you expect the variables to have a certain value (last object in the array), but suppose the array is empty? GDB is your friend, and is even better in Xcode 4.1
精彩评论