清除而不是释放 NSMutableString
我有一个相当大的循环,它获取一个字符串,对其执行一些操作,然后再进入下一个字符串。我最初是释放它然后重新分配它,但认为这有点浪费资源,但不知道如何清除它以重用它。
I've got a rather large loop that gets a string, does something to it, than goes onto the next one. I was originally releasing it then reallocating it but thought that is a bit of waste of resources but can't figure out how to just clear it out to reuse it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
所选的解决方案将崩溃并出现以下错误:
“尝试使用 setString 改变不可变对象:”
这对我有用:
self.myString = [NSMutableString stringWithString:@""];
确保你在你的类中综合了 myString 。
The selected solution will crash with the following error:
'Attempt to mutate immutable object with setString:'
This worked for me instead:
self.myString = [NSMutableString stringWithString:@""];
make sure you synthesize myString in your class.
一种方法是
[myString setString: @""]
。One way would be
[myString setString: @""]
.