8

I've looked everywhere for this, online, on stack overflow and cannot still work out what I'm doing wrong.

I'm trying to add an element to an existing NSMutableArray. But it crashes on line 4:

-[__NSArrayI addObject:]: unrecognized selector sent to instance 0x897b320

The code:

NSMutableArray *mystr = [[NSMutableArray alloc] init];

mystr = [NSArray arrayWithObjects:@"hello",@"world",@"etc",nil];

NSString *obj = @"hiagain";

[mystr addObject:obj];

What am I doing wrong? This is driving me crazy!!!

4
  • 2
    Why the downvotes? - it seems a reasonable question to me , just because someone does not know the NS API, doesent make it a bad question Commented Nov 29, 2012 at 16:32
  • 1
    @gheese I don't get it either. It's a clear question. It provides the specific code and a specific error message. Downvoting without a reason shouldn't be allowed. It helps no one. I think it's just some grumpy guys that think just because they know this stuff, they should downvote what appears to be a silly question to them. Commented Nov 29, 2012 at 16:36
  • I'm not one of the down-voters, but I would suggest the downvotes are because : 1. There has seemingly been no effort to look up the error message (unrecognized selector). 2. If the error message had been looked up, it should have led to believe that the NSArray does not respond to that selector. This would have led to 3. to look up the NSArray documentation which would reveal that it has no such selector. Commented Nov 29, 2012 at 16:39
  • 1
    @Nick Bull - agreed, but one must remember that for newbies "unrecognized selector sent to instance 0x897b320" wouldn't mean a thing. Also I suspect that 50 % of the questions posted here wouldnt meet all the criteria you state Commented Nov 29, 2012 at 17:06

5 Answers 5

14

You array is not mutable!. Use NSMutableArray

mystr = [NSMutableArray arrayWithObjects:@"hello",@"world",@"etc",nil];

You get unrecognized selector since NSArray does not contain the addObject method

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

1 Comment

@Imme22009 When you do this, you don't need the call to [[NSMutableArray alloc] init] on the first line.
3

Your code should be:

NSMutableArray *mystr = [[NSMutableArray alloc] initWithObjects:@"hello",@"world",@"etc",nil];

NSString *obj = @"hiagain";

[mystr addObject:obj];

Comments

0

The second line you're reassigning an instance of NSArray rather of NSMutableArray to your mystr variable.

Try something like this:

NSMutableArray *mystr = [NSMutableArray  arrayWithObjects:@"hello",@"world",@"etc",nil];

[mystr addObject:@"hiagain"]

Comments

0

You originally create mystr as a mutable array, but then assign it to a standard NSArray in the next line. Instead of calling "arrayWithObjects," add each item using "addObjects" or some other function that doesn't create a new immutable array.

Comments

0

Ahhhhh spotted it already

Line 2 should be:

[NSMutableArray arrayWithObjects:@"hello",@"world",@"etc",nil];

Sorry to waste your time with that!

1 Comment

I think you mean NSMutableArray!

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.