在数组中添加字符串对象

在给NSMutableArray添加元素的时候,在第四行出现错误:

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

代码:

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

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

NSString *obj = @"hiagain";

[mystr addObject:obj];

不知道哪出错,感谢帮忙。

问题出在数组上,你用的数组是固定的,改用NSMutableArray

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

错误中提到的unrecognized selector是由于NSArray中不包含addObject方法

换这个代码试试:

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

NSString *obj = @"hiagain";

[mystr addObject:obj];