错误:'尝试使用appendString改变不可变对象:' -

时间:2011-03-08 13:33:35

标签: iphone objective-c nsmutablearray nsmutablestring

我收到了错误消息 '尝试使用appendString:'

来改变不可变对象

我的代码是

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


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

}  

ArrayValue是NSMutableArray 我无法理解问题在哪里

提前谢谢

3 个答案:

答案 0 :(得分:7)

发布后,您所拥有的代码不会向您提供您描述的错误。可能在分配resultString和for循环之间,你用普通的NSSring覆盖它。

答案 1 :(得分:2)

就是这样:

对我有用......

    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:

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

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

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

答案 2 :(得分:2)

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

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

}  

对我有用..