ullmark
ullmark

Reputation: 2479

Am I using NSUserDefaults wrong?

I've created a "Settings" model for my iPhone app. It only contains two properties and a class method for loading and one instancemethod for saving.

I load it as follows

+ (UserSettings *)getCurrent {
    NSUserDefaults *userPrefs = [NSUserDefaults standardUserDefaults];
    UserSettings *settings = [UserSettings new];
    settings.username = [userPrefs stringForKey:kUserNameKey];
    settings.password = [userPrefs stringForKey:kPasswordKey];
    [userPrefs release];
    return settings;
}

The problem that the handling with the NSUserDefault casts an exception;

-[NSCFArray objectForKey:]: unrecognized selector sent to instance 0x50b220

I have imported my header with the constants which are defined as follows;

#define kUserNameKey    @"Username"
#define kPasswordKey    @"Password"

(I am aware of keychain and am planning on changing to that later on, but want to solve the userdefaults)

Upvotes: 4

Views: 2228

Answers (2)

How are your UserSettings properties defined for username/password? They should be set to "copy" for strings.

Upvotes: 0

cobbal
cobbal

Reputation: 70795

try removing the [userPrefs release] since you never retained or alloced it

Upvotes: 7

Related Questions