Modifying an input NSString value in a Objective-C method
In terms of memory management, is it correct to modify the input variable content
in the fol开发者_如何学JAVAlowing 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!
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 the content
object at all. NSString
is immutable, so you can't do that, you're creating a new instance and assigning it to the variable.
As content
variable is your local variable, so it is correct. You only change memory address that is stored in variable content
. Don't forget to release memory, that you pass in variable content
.
精彩评论