I’m having trouble storing and retrieving a float in NSUserDefauts. I store the value, but when I retrieve it, it returns 0.
here’s what I tried and didn’t work:
[pref setFloat:3.0f forKey:@"key"];
float value = [pref floatForKey:@"key"]; //value=0
[pref setFloat:3 forKey:@"key"];
float value = [pref floatForKey:@"key"];//value=0
[pref setObject:[NSNumber numberWithFloat:3] forKey:@"key"];
float value = [[pref objectForKey:@"key"]floatValue];//value=0
[pref setObject:[NSNumber numberWithFloat:3.0f] forKey:@"key"];
float value = [[pref objectForKey:@"key"]floatValue];//value=0
What am I doing wrong here? I’ve tried these 4 pieces of code but they all return zero when retrieving the float from NSUserDefaults.
Any help is appreciated.
Thanks!
The first example in your code is fine, assuming that this line:
Appears before it. As I suggested in my comment, the behaviour you are seeing suggests that
prefis nil.