修改 Objective-C 方法中的输入 NSString 值
在内存管理方面,下面的方法修改输入变量content
是否正确?
- (NSMutableArray *) extractResults:(NSString *)content {
...
regex = [NSRegularExpression ...];
content = [regex stringByReplacingMatchesInString:content
options:0
range:NSMakeRange(0, [content length])
withTemplate:@""];
...
}
在这种特殊情况下,我不关心该值是否在方法范围之外保持修改。只是想知道该分配是否会产生内存泄漏。
谢谢!
In terms of memory management, is it correct to modify the input variable content
in the following method?
- (NSMutableArray *) extractResults:(NSString *)content {
...
regex = [NSRegularExpression ...];
content = [regex stringByReplacingMatchesInString:content
options:0
range:NSMakeRange(0, [content length])
withTemplate:@""];
...
}
In this particular case I do not care if the value remains modified outside the method scope. Just wondering if that assignment produces a memory leak.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不会,它不会产生泄漏,因为
stringByReplacing...
的返回值是自动释放的。但是,您应该意识到您根本没有修改content
对象。NSString
是不可变的,所以你不能这样做,你正在创建一个新实例并将其分配给变量。No, it doesn't produce a leak because the return value of
stringByReplacing...
is autoreleased. However, you should be aware that you are not modifying thecontent
object at all.NSString
is immutable, so you can't do that, you're creating a new instance and assigning it to the variable.由于
content
变量是您的本地变量,因此它是正确的。您只需更改存储在变量content
中的内存地址。不要忘记释放您传入变量content
的内存。As
content
variable is your local variable, so it is correct. You only change memory address that is stored in variablecontent
. Don't forget to release memory, that you pass in variablecontent
.