0

I am getting "-[__NSArrayI addObject:]: unrecognized selector sent to instance 0x10dd15ee0" here, when I try to addObject to an NSMutableArray. I'm not sure why.

I have a set of custom classes, that are nested. WTEvent has an NSMutableArray property called mats that consists of WTMat objects. mats has an NSMutableArray of WTBout objects called bouts.

I want to be able to update one bout in bouts while leaving the rest of the bouts alone. Here, I locate the mat that I need to assign a bout to. If it's not initialized, I initialize it. Then, if it's empty, I try to add a newBout to it, and it crashes.

Shouldn't I be able to change elements of a NSMutableArray like this?

// Locate the correct mat
WTMat* mat = [self getMatFromBoutKey:[updateData valueForKey:@"boutKey"]];
WTBout* newBout = [WTBout buildBoutFromDictionary:updateData];

// need to initialize bouts
if ([mat bouts] == nil) {
    NSLog(@"Initializing bouts");
    NSMutableArray* newBouts = [[NSMutableArray alloc] init ];
    [mat setBouts:newBouts];
}

if ([[mat bouts] count]) {
    // if bouts has bouts, then adjust the bout stack

    NSLog(@"bouts is not empty");
} else {
    // if bouts is empty, just add a bout

    NSLog(@"Adding a newBout");
    NSMutableArray* oldBouts = [mat bouts];
    NSLog(@"oldbouts: %@", [oldBouts description]);
    [oldBouts addObject:newBout]; // -[__NSArrayI addObject:]: unrecognized selector sent to instance 0x10dd15ee0

}

WTMat.h:

#import <Foundation/Foundation.h>

@interface WTMat : NSObject
{
    NSString* matName;
    NSString* boutKey;
    NSString* multicastAddress;
    NSMutableArray* bouts;
}

@property (strong, nonatomic) NSString* matName;
@property (strong, nonatomic) NSString* boutKey;
@property (strong, nonatomic) NSString* multicastAddress;
@property (copy, nonatomic) NSMutableArray* bouts; 

@end

Log:

2012-05-12 08:14:00.491 Wrestling Tools[12623:403] Initializing bouts
2012-05-12 08:14:00.492 Wrestling Tools[12623:403] Adding a newBout
2012-05-12 08:14:00.492 Wrestling Tools[12623:403] oldbouts: (
)
2012-05-12 08:14:00.492 Wrestling Tools[12623:403] -[__NSArrayI addObject:]: unrecognized selector sent to instance 0x10dd15ee0
2012-05-12 08:14:00.492 Wrestling Tools[12623:403] -[__NSArrayI addObject:]: unrecognized selector sent to instance 0x10dd15ee0
2012-05-12 08:14:00.494 Wrestling Tools[12623:403] (
    0   CoreFoundation                      0x00007fff96783fc6 __exceptionPreprocess + 198
    1   libobjc.A.dylib                     0x00007fff92375d5e objc_exception_throw + 43
    2   CoreFoundation                      0x00007fff968102ae -[NSObject doesNotRecognizeSelector:] + 190
    3   CoreFoundation                      0x00007fff96770e73 ___forwarding___ + 371
    4   CoreFoundation                      0x00007fff96770c88 _CF_forwarding_prep_0 + 232
    5   Wrestling Tools                     0x000000010dc49343 -[WTUpdater fullUpdate:] + 835
2
  • Type po 0x10dd15ee0 in debugger to see what object is being sent the wrong selector. Commented May 12, 2012 at 13:42
  • I see the issue is that I have the bouts property set to 'copy', which gives me an immutable copy, which doesn't know the method addObject: I have changed it back to strong, like my other properties, but I'm not sure what kind of havoc this might wreak on on my application. How can I manage mutable arrays that are being accessed from multiple instances of other classes. Is this going to be a nightmare? Commented May 12, 2012 at 13:45

3 Answers 3

5

This is your problem:

@property (copy, nonatomic) NSMutableArray* bouts; 

The "copy" parameter makes an immutable copy of the array, which means when this line runs:

NSMutableArray* oldBouts = [mat bouts];

You're actually getting an ordinary NSArray, which is why you get the unrecognised selector error.

The easy solution to this is to change the "copy" keyword to "strong". Slightly harder would be leave it as copy but to instead change the type of the property to NSArray, though that would complicate modifying the array.

Finally, you could make your own implementation of setBouts: which does do a copy using the -mutableCopy method to ensure the copy it makes is mutable.

Sign up to request clarification or add additional context in comments.

Comments

0

I think it's all about your NSMutableArray* oldBouts = [mat bouts]; Alloc it, and see what will happen.

Comments

-1

try initializing properties using @dynamic. A very similar answer may help you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.