I’ve been having a really weird problem. I have a mutable array that is claiming to be mutable and claiming to respond to addObject:, but crashes saying that its not mutable.
Here is the code:
NSLog(@"Can add object: %d", [[MySingleton sharedInstance].myArray respondsToSelector:@selector(addObject:)]);
if([[MySingleton sharedInstance].myArray isKindOfClass:[NSMutableArray class]])[[MySingleton sharedInstance].myArray addObject:objectToAdd];
else NSLog(@"Not mutable");
Now, if I set it to mutable copy, it works.
NSLog(@"Can add object: %d", [[MySingleton sharedInstance].myArray respondsToSelector:@selector(addObject:)]);
if([[MySingleton sharedInstance].myArray isKindOfClass:[NSMutableArray class]])[[MySingleton sharedInstance].myArray.mutableCopy addObject:objectToAdd];
else NSLog(@"Not mutable");
Why is that? Why is it claiming to be mutable and able to addObject:, but crashing unless I use a mutable copy?
This should work out a bit better, I hope. Maybe if I write enough, this will come through as a proper answer for now. The answer is in the comments of the main post. I needed to check isMemberOfClass to check if it inherited the class methods, rather than checking if it was of a certain class type, since it was mutable, but didn’t inherit the mutable methods.