Am I using NSUserDefaults incorrectly?

I created the Settings model for my iPhone application. It contains only two properties and a class method for loading and one example 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 is that processing with NSUserDefault throws an exception;

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

I imported my header with constants, which are defined as follows:

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

(I know about keychain and plan to switch to it later, but I want to solve userdefaults)

+4
source share
2 answers

try deleting [userPrefs release] since you never saved or allocated it

+7
source

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

0
source

All Articles