本文介绍了尝试改变不可变对象错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的代码在此行崩溃:
[(NSMutableString *)string replaceCharactersInRange:range withString:@""];
尝试更改不可变对象时出错.
with the error attempt to mutate immutable object.
这是怎么回事,我该如何解决?
How is this happening and how do i fix it?
推荐答案
字符串不可变,强制转换不是魔术,也不能将其转换为可变字符串.您应该在可变副本上执行此操作:
The string isn't mutable, casting is not magic, it wouldn't turn it into a mutable string. You should do it on a mutable copy:
NSMutableString* mutableString= [string mutableCopy];
[mutableString replaceCharactersInRange:range withString:@""];
这篇关于尝试改变不可变对象错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!