should variable be released or not? iphone-sdk

Posted by psebos on Stack Overflow See other posts from Stack Overflow or by psebos
Published on 2010-03-20T16:59:24Z Indexed on 2010/03/20 17:01 UTC
Read the original article Hit count: 179

Filed under:
|
|

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!!!!

© Stack Overflow or respective owner

Related posts about iphone-sdk

Related posts about iphone