error : 'Attempt to mutate immutable object with appendString:' --

南笙酒味 提交于 2019-12-06 02:09:08

As posted, the code you have will not give you the error you describe. Probably, somewhere between allocating resultString and the for loop, you are overwriting it with a normal NSSring.

Just do have like this:

It works for me...

    NSMutableString *resultString= [[NSMutableString alloc]init];
NSMutableArray *ArrayValue=[[NSMutableArray alloc]init];
[ArrayValue addObject:@"One"];
[ArrayValue addObject:@"Two"];
[ArrayValue addObject:@"Three"];

for (NSMutableString *s in ArrayValue)
{
    [resultString appendString:s];
    NSLog(@"%@",resultString);------->You should use %@ to print the string otherwise will show your warning.

}  

O/P on Console:

2011-03-08 19:13:02.243 iPadMables[4557:207] One

2011-03-08 19:13:06.224 iPadMables[4557:207] OneTwo

2011-03-08 19:13:09.388 iPadMables[4557:207] OneTwoThree

    ArrayValue = [NSMutableArray arrayWithObjects:@"b",@"o",@"n",nil];
NSMutableString *resultString= [[NSMutableString alloc]init];

for (NSMutableString *s in self.ArrayValue)
{
    [resultString appendString:s];
    NSLog(resultString);

}  

works for me..

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!