Objective c add string object to an array Objective c add string object to an array arrays arrays

Objective c add string object to an array


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


Your code should be:

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


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"]