It depends on how you declare it. You should read the documentation in memory management. Basically the rules:
NSString *aString = [NSString stringWithString:@"Hello"]; NSString *bString = [NSString stringWithFormat:@"%@", @"Hello"];
In these cases, the string is not copied or saved. It is auto-implemented, which means it will be automatically released the next time the auto-ad pool is depleted. You do not need to call the release method on them. (Therefore, assigning a new value will not leak it.)
NSString *cString = [[NSString alloc] initWithFormat:@"%@", @"Hello"]; [cString release];
According to the Objective-C convention, methods that use alloc and have an account to save one are not auto-implemented, so you need to explicitly free them. Assigning a new value without releasing the old will result in a leak.
You can also explicitly call the copy method or the save method on a string. In any case, the new line will have a storage counter of 1 and will not be auto-implemented, so you will need to call the release method on it before assigning a new value.
NSString *dString = [cString retain]; NSString *eString = [cString copy]; ... [dString release]; [eString release];
If this property is, and you use self.variableName, it will take care of you (through the getters and setters that are generated using @synthesize). If you do this explicitly, you should definitely call release on the variables that you called saved or copied.
Edit: As some commentators have noted below, thinking about management in terms of “ownership” is usually preferable to describe these ideas rather than keep an account.
source share