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)
From stackoverflow
-
try removing the
[userPrefs release]
since you never retained or alloced itullmark : I was under the impression that the "standardUserDefaults" function had to alloc it. That seems to have been my problem though. Thankscobbal : rule of thumb: if and only if you alloc, retain, or copy it then release or autorelease itChuck : All you need to know about Objective-C memory management: http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html -
How are your UserSettings properties defined for username/password? They should be set to "copy" for strings.
0 comments:
Post a Comment