Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 476
Zak Avatar asked Jul 10 '10 17:07

Zak


1 Answers

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.

like image 71
Georg Fritzsche Avatar answered Oct 15 '22 12:10

Georg Fritzsche