3

I am getting an error 'Attempt to mutate immutable object with appendString:'

and my code is

NSMutableString *resultString= [[NSMutableString alloc]init];


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

}  

ArrayValue is NSMutableArray.
I am not able to understand where is the problem

thank you in advance

JeremyP
  • 84,577
  • 15
  • 123
  • 161
Pooja
  • 2,162
  • 5
  • 33
  • 64
  • When you reduce code so you can post it you should make sure that the snippet you post gives the same result as the whole code. – Matthias Bauch Mar 08 '11 at 13:57

3 Answers3

7

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.

JeremyP
  • 84,577
  • 15
  • 123
  • 161
2

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

Ajay Sharma
  • 4,509
  • 3
  • 32
  • 59
2
    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..

makboney
  • 1,808
  • 1
  • 17
  • 34
  • Array value is coming from web server and i am using same method in other view and its work fine – Pooja Mar 08 '11 at 13:57