should variable be released or not? iphone-sdk
- by psebos
Hi, I have the following piece of code from a book.
There is this function loadPrefs where the NSString *userTimeZone is being released before the end of the function.
Why? The string was not created with alloc and I assume that the stringForKey function returns an autoreleased NSString. Is this an error or am I missing something? Is it an error in the book? (I new into objective-C)
In the documentation for stringForKey the only thing it mentions is:
Special Considerations
The returned
string is immutable, even if the value
you originally set was a mutable
string.
The code:
- (void) loadPrefs {
timeZoneName = DefaultTimeZonePref;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *userTimeZone = [defaults stringForKey: TimeZonePrefKey];
if (userTimeZone != NULL)
timeZoneName = userTimeZone;
[userTimeZone release];
show24Hour = [defaults boolForKey:TwentyFourHourPrefKey];
}
Thanks!!!!