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

发布于 2024-10-21 00:11:05 字数 324 浏览 0 评论 0原文

我收到错误 “尝试使用appendString改变不可变对象:”

我的代码是

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


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

}  

ArrayValue is NSMutableArray。
我无法理解问题出在哪里,

提前谢谢您

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

世态炎凉 2024-10-28 00:11:05

正如发布的那样,您拥有的代码不会给您所描述的错误。也许,在分配 resultString 和 for 循环之间的某个地方,您正在用普通的 NSSring 覆盖它。

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.

无戏配角 2024-10-28 00:11:05

就像这样:

它对我有用...

    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] One

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

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

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

我们的影子 2024-10-28 00:11:05
    ArrayValue = [NSMutableArray arrayWithObjects:@"b",@"o",@"n",nil];
NSMutableString *resultString= [[NSMutableString alloc]init];

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

}  

为我工作..

    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..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文