I need to store a numeric parameter in NSUserDefault, this parameter must be as default 1
So i write this code :
NSNumber *one = [NSNumber numberWithInt:100];
NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
one,@"my-param",
nil];
[def registerDefaults:dict];
This parameter has a bind over a checkbox in IB so i can check that it’s correctly set and when i start my app i see checkbox with state “on”.
On the other side i must check this value programmatically so… i do something like :
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSInteger my_param = [defaults integerForKey:@"my-param"];
I excepted that since the value is not set by the user this function return value that i set as default (in my case “1”) but with my surprise i found that if value has never been set by user it returns 0 … as you can understand this’s terrible 😛 because now i can’t understand if this “0” is obtained by user choice of this’s a consequence of a non-set value… how can i write code to manage this situation ?
To store the numeric values in NSUserDefaults you can simply directly use as follows:
To set the integer Value in NSUserDefault use as follows below:
And to use that NSInteger value anywhere in your project use as below:
NSInteger linteger = [[NSUserDefaults standardUserDefaults] integerForKey:@”integerkey”];
And i didnt understand your question exactly, but anyhow i think this above code may help you.