NSMutableArray addObject: -[__NSArrayI addObject:]: unrecognized selector sent to instance

Zak picture Zak · Jul 10, 2010 · Viewed 53.5k times · Source

I have tried to initialize my NSMutableArray 100 ways from Sunday, and NOTHING is working for me. I tried setting it equal to a newly allocated and initialized NSMutableArray, just allocating, initializing the variable by itself, every combination I could think of and always the same result.

Here's the code:

Object.h

NSMutableArray *array;

@property (copy) NSMutableArray *array;

Object.m

@synthesize array;

if ( self.array ) {
    [self.array addObject:anObject];
}
else {
    self.array = [NSMutableArray arrayWithObjects:anObject, nil];
}

NOTE: In debug "anObject" is NOT nil at time of execution...

I have tested anObject and it isThe initialization works just fine, but I keep getting the error below when I try to addObject: to self.array.

2010-07-10 11:52:55.499 MyApp[4347:1807] -[__NSArrayI addObject:]: unrecognized selector sent to instance 0x184480

2010-07-10 11:52:55.508 MyApp[4347:1807] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI addObject:]: unrecognized selector sent to instance 0x184480'

Does anyone have any idea what's going wrong?

Answer

Georg Fritzsche picture Georg Fritzsche · Jul 10, 2010

The synthesized setter for @property (copy) sends a copy message to the array, which results in an immutable copy.

You have no choice but the implement the setter yourself here, as detailed in the Objective-C guide.