Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective c add string object to an array

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!!!

like image 430
Imme22009 Avatar asked Nov 29 '12 15:11

Imme22009


1 Answers

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

like image 93
giorashc Avatar answered Oct 04 '22 21:10

giorashc