1

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?

Daniel Sun
  • 41
  • 2
  • 1
    Could you show us declaration of property mArray please ? – hsarret Jul 08 '15 at 15:41
  • Assuming your `mArray` property is defined with `copy`, this is a duplicate of http://stackoverflow.com/questions/14856681/why-does-a-copy-nonatomic-nsmutablearray-property-create-nsarrays – rmaddy Jul 08 '15 at 16:43
  • My property declaration looks like this: @property (strong, nonatomic) NSMutableArray *mArray; – Daniel Sun Jul 08 '15 at 19:25

1 Answers1

0

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

rounak
  • 9,217
  • 3
  • 42
  • 59