views:

935

answers:

2

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)

+4  A: 

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

cobbal
I was under the impression that the "standardUserDefaults" function had to alloc it. That seems to have been my problem though. Thanks
ullmark
rule of thumb: if and only if you alloc, retain, or copy it then release or autorelease it
cobbal
All you need to know about Objective-C memory management: http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html
Chuck
A: 

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

Kendall Helmstetter Gelner