Alex Rivera | Logout

Objective c add string object to an array

Asked 2012-11-29T15:56:51.780
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!!!

Edit
Report

1 Answer

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.

answered 2012-11-29T16:00:05.560

Your Answer