尝试更改不可变对象时发生错误 [英] ERROR for attempting mutating immutable object

查看:104
本文介绍了尝试更改不可变对象时发生错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是什么错误?我正在使用可变类型,但仍然无法正常工作!

What is this error? I am using mutable type but still it is not working!

错误是:

由于未捕获的异常而终止应用程序 'NSInvalidArgumentException',原因:'不可变突变 具有deleteCharactersInRange的对象:'

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'mutate immutable object with deleteCharactersInRange:'

NSMutableString *line= [NSMutableString stringWithString:@"here is the string"];
[line deleteCharactersInRange: NSMakeRange(1,9 )];

推荐答案

我的新答案:

My new answer:

使用您的新代码,我在最后添加了一行:

With your new code, I added one extra line at the end:

NSMutableString *line= [NSMutableString stringWithString:@"here is the string"];
[line deleteCharactersInRange: NSMakeRange(1,9 )];
NSLog( @"line is %@", line );

控制台输出为:

2011-11-25 06:08:13.248 TestingCommandLine[13324:903] line is he string

没有引发异常.看起来对我有用!

No exception thrown. Looks like it's working for me!

我的原始答案(针对您的原始问题):

您不能从其中没有任何内容的行中删除字符(因此是例外).

You can't delete characters from a line that has nothing in it (hence the exception).

在这两行源代码之间,您是否还有其他代码?如果没有,则您有一个有效的NSMutableString ...,其中没有任何内容.而且,您不能从其中没有任何内容的可变字符串中删除任何内容.

Do you have any additional code between those two lines of source there? If not, you have a valid NSMutableString... with nothing in it. And you can't delete anything from a mutable string that has nothing in it.

这篇关于尝试更改不可变对象时发生错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆