NSMutableArray becomes immutable when reinstantiated?

时间:2015-07-08 15:37:06

标签: ios objective-c arrays nsmutablearray nsarray

I've made a NSMutableArray a property on my view controller, which holds some core data objects for users.

When the user presses a button , I clear out the contents of the mutable array with a while loop,

while (self.mArray.count != 0){ 
   [context deleteObject:self.mArray[0]];
   [self.mArray removeObjectAtIndex:0];
}

After the while loop, I reinit the array:

self.mArray = [[NSMutableArray alloc] init];

I know it isn't necessary since there should be zero objects left in the array, but regardless, when I reinitialize the array, and then check the class of the array in the debugger, I get __NSArrayI, which is corroborated by an exception thrown when I attempt to add an object into self.mArray right afterwards.

I've looked for any other references to my array, but I've always passed around [self.mArray mutableCopy] as arguments to other methods, and I never cast it as an NSArray. I just don't understand how calling [[NSMutableArray alloc] init] would initialize the array as an immutable array.

What am I missing?

1 个答案:

答案 0 :(得分:0)

It's likely that you have declared mArray as copy in the property declaration. Change that to strong.