I’m developing an iOS application with latest SDK and XCode.
This is a simple question but I don’t know how to do it because I don’t want any memory leaks on my code.
I’m using ARC on my project and I have the following header declaration:
@interface UserPreferences : NSObject
{
NSUserDefaults* prefs;
}
@property (nonatomic, readonly) NSString* appLanguage;
// More code
- (void) setAppLanguage:(NSString *)appLanguage;
// More code
@end
And this is how I’ve implemented - (void) setAppLanguage:(NSString *)appLanguage;.
- (void) setAppLanguage:(NSString *)newAppLanguage
{
[prefs setObject:appLanguage forKey:APP_LANGUAGE_KEY];
appLanguage = [NSString stringWithString:newAppLanguage];
}
Is appLanguage = [NSString stringWithString:newAppLanguage]; correct?
I don’t know it appLanguage will have a value when I set a new one to it.
Is my code correct?
Your code doesn’t have any leaks; ARC automatically releases the previous value for
appLanguagefor you. I would writeappLanguage = [newAppLanguage copy]rather than usingstringWithString:, but the effect is the same.