Reputation: 37581
I'm getting an unrecognized selector exception attempting to call addObject on an array. However my array is an NSMutableArray, not an NSArray. I don't understand why this is happening.
I have the following classes:
@interface A : NSObject
@property (nonatomic, copy) NSMutableArray* children;
- (id) init;
- (void) addChild: (A*) child;
@end
@interface B : A
- (id) init;
@end
@implementation A
- (id) init
{
self = [super init];
return self;
}
- (void) addChild: (A*) child
{
[self.children addObject:child];
}
@end
@implementation B
-(id) init
{
self = [super init];
if (self != nil)
{
self.children = [[NSMutableArray alloc] init];
}
return (self);
}
Then if I create an object of type B and call [b addChild: anObject] I'm getting an unrecognized selector and I don't understand why.
The output is:
2013-04-08 14:31:08.046 otest[13381:7e03] -[__NSArrayI addObject:]: unrecognized selector sent to instance 0x17759c0
What is the problem?
TIA
(Please focus on the actual code and problem, not side discussions why I'm not allocating the array in A's init, unless relevant. I want to understand why the code as it stands does't work. Thanks).
Upvotes: 0
Views: 206
Reputation: 43330
By declaring the array with the storage qualifier copy
instead of strong
, your mutable array will be copied on assignment and turned into an immutable NSArray.
Upvotes: 3