1

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?

RileyE
  • 10,874
  • 13
  • 63
  • 106
  • 1
    http://stackoverflow.com/questions/1096772/is-it-safe-to-use-iskindofclass-against-an-nsstring-instance-to-determine-type – GendoIkari Jun 20 '12 at 18:37
  • Oh. For some reason I had the two backwards. I thought isMemberOfClass was the one that was unsafe. I just want to know why it says that it responds to the selector. Isn't that for instantiation only, not type of class? – RileyE Jun 20 '12 at 18:44
  • Or would I have to user instancesRespondToSelector:? – RileyE Jun 20 '12 at 18:53
  • Well, Gendolkari posted the correct response: http://stackoverflow.com/questions/1096772/is-it-safe-to-use-iskindofclass-against-an-nsstring-instance-to-determine-type – RileyE Jun 30 '12 at 00:17

1 Answers1

0

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.

RileyE
  • 10,874
  • 13
  • 63
  • 106