在iPhone上正确重用NSMutableString

0 iphone memory-management objective-c stringwithformat nsmutablestring

在iPhone上使用objective-c,这段代码出了什么问题?是在泄漏记忆吗?为什么?我该如何正确地做到这一点?

NSMutableString *result = [NSMutableString stringWithFormat:@"the value is %d", i];
Run Code Online (Sandbox Code Playgroud)

...然后在我的代码中...我可能需要将其更改为:

result = [NSMutableString stringWithFormat:@"the value is now %d", i];
Run Code Online (Sandbox Code Playgroud)

我需要第二次使用stringWithFormat ...但是不是创建一个新字符串并且没有正确释放旧字符串吗?

Ole*_*ann 5

不,它不会泄漏内存,因为stringWithFormat:返回一个自动释放的对象.