Ok, since i’m new to obj-c and have a background in windows developing, i’m searching for the counterpart of Regedit.
I’ve understand that i should use NSUserDefaults, right?
So, i have created two functions, one for setting and one for getting values. They look like this: (And yea… ignore my silly functions names) 🙂
//SET:
-(void)SetRegeditValue:(NSString*)Name:(NSString*)Value
{
NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:Value,Name, nil];
[ud registerDefaults:dict];
}
//GET:
-(NSString*)GetRegeditValue:(NSString *)Name
{
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *get = [prefs stringForKey:Name];
if(get)
return get;
else
return @"Not found!";
}
And i use them like this:
//TO SET:
SetRegitValue:@"my_value":@"my_value_name";
//TO GET:
GetRegeditName:@"my_value_name";
I don’t know if this is the “right” way to do it, but it works.
The only thing is that when i kill the app, it does not remember these values and i get “Not found!”. Is there something i have to set so it REALLY saves these variables?
Oh, i’m building a cocoa application.
The
registerDefaults:method is used to assign default values which get returned when you ask for an undefined key. To actually store a value, you need to use one of theset*:forKey:methods. Specifically, you should usesetObject:forKey:since you are saving strings, which are objects.I used the same argument order that your code did, but note that it does not match the example you showed, which passed the value first.