NSMutableArray addObject: - [__ NSArrayI addObject:]: unrecognized selector sent to the instance

I tried to initialize my NSMutableArray 100 ways from Sunday, and NOTHING works for me. I tried to set it equal to the newly allocated and initialized NSMutableArray, simply selecting, initializing the variable by itself, every combination that I could think of, and always the same result.

Here is 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 the debug "anObject" NOT nil at runtime ...

I tested anObject and this initialization works fine, but I keep getting the error below when I try to add an object: 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] *** Application terminated due to the uncaught exception 'NSInvalidArgumentException', reason: '- [__ NSArrayI addObject:]: unrecognized selector sent to instance 0x184480'

Does anyone know what is going wrong?

+66
iphone nsmutablearray
Jul 10 '10 at 17:17
source share
8 answers

I would like to feed my hat to Georg Fritzsche. In the end, I needed to use (copy) instead of (save), and I would not know what to do without entering it.

 //@property (copy) NSMutableArray *array; @property (nonatomic, copy) NSMutableArray *array; //overridden method is non-atomic as it is coded and should be reflected here. 

If you want to use (copy) on a mutable object, you must override the "setter" method as follows:

 - (void)setArray:(NSArray *)newArray { if ( array != newArray ) { [array release]; array = [newArray mutableCopy]; // [array retain]; // unnecessary as noted by Georg Fritzsche } return; } 

NOTE. You will get a compiler warning: Incompatible Objective-C type of initialization of the NSArray * structure expected by 'struct NSMutableArray *' I decided to declare the newArray parameter as (NSArray *), because you are given the flexibility to let any array pass and correctly copy your variable (NSMutableArray *) . If you want to declare the newArray parameter as (NSMutableArray *), you still need to leave the mutableCopy method in place to get the desired results.

Greetings to George! Z @K!

+11
Jul 10 '10 at 19:56
source share

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

You have no choice but to implement the installer here, detailed in the Objective-C manual.

+81
Jul 10 '10 at 17:22
source share

Since I witnessed the reading of my message, an idea occurred to me, and I answered my question. This resolution was rather obscure, and I decided to go ahead, create a post and answer it myself (so that any other newcomers, like me, will not depend).

My mistake was in ...

 @property (copy) NSMutableArray *array; 

he should have been ...

 @property (retain) NSMutableArray *array; 

The error did not occur in the way I executed my code, but rather in the way anObject tried to "copy" the NSMutableArray array.

As we all know ...

 mutableArray = [mutableArray copy]; 

not always (or ever, in my experience) equal to ...

 mutableArray = [mutableArray mutableCopy]; 

And that was the cause of my problem. Just by switching @property from (copy) to (save), I solved my problem.

+68
Jul 10 '10 at 17:27
source share

I was getting the same error, although my properties were strong (using ARC), and I allocated the array using NSMutableArray.

What happened was that I archived the modified array (since it contains user objects) for future use, and when it is decrypted, it returns an unaltered copy.

Hope this helps someone out there.

+3
Jan 12 '12 at 19:20
source share

Have some indexes (in a data array elsewhere) and want to have them in numerical order (for good reason). Failure to add mutableCopy to the end. I was completely puzzled until I reminded that using Objective-C literal @ [] returns an immutable array .

 NSMutableArray *a = [@[@(self.indexA), @(self.indexB)] mutableCopy]; NSLog(@"%@", a); [indexArray sortUsingComparator: ^(NSNumber *obj1, NSNumber *obj2) { return [obj1 compare:obj2]; }]; NSLog(@"%@", a); 

Thanx, Zak!

+3
Feb 16 '13 at 22:17
source share

I was bitten by this exception for a typo that I made, maybe it will save someone 5 minutes. of his time:

I wrote:

 NSMutableArray *names = [NSArray array]; 

instead:

 NSMutableArray *names = [NSMutableArray array]; 

The compiler has no problem with this because NSMutableArray is also an NSArray, but it crashes when trying to add an object.

0
Mar 03 '13 at 12:40
source share

The error occurred due to an attempt to add an object to the type NSMutableArray, which actually pointed to an NSArray object. This type of script is shown in some demo code below:

 NSString *test = @"test"; NSMutableArray *mutableArray = [[NSMutableArray alloc] init]; [mutableArray addObject:test]; NSArray *immutableArray = [[NSArray alloc] init]; mutableArray = immutableArray; [mutableArray addObject:test]; // Exception: unrecognized selector 

From the code above it is easy to see that the type of the subclass is assigned to the class of the superclass. For example, in Java, this will immediately be flagged as an error (conversion error between types), and the problem will be resolved fairly quickly. To be honest with Objective-C, a warning when trying to perform an incompatible assignment, however, it just simply does not seem enough sometimes, and the result can be a real pain for developers. Fortunately, this time, I myself could not bear most of this pain: P

0
Jul 30 '14 at 9:36
source share

This exception may occur if one of the objects in the array is null.

-four
Nov 16 '11 at 9:27 a.m.
source share



All Articles