0
votes

I am building an application in which I want to save some user data by using NSUserDefaults, and it is a property of one of my controllers as following:

  @property (nonatomic, strong) NSUserDefaults *userPreference;
  @synthesize userPreference = _userPreference;

And I am trying to save something called a scale that should be a float, so I do something like this in the getter to put in some default values of scale in case user did not enter one:

  if (!_userPreference) {
      _userPreference = [NSUserDefaults standardUserDefaults];
  }

  if (![_userPreference floatForKey:@"scale"]) { 
      // if user has not entered this info yet
      // also 0 is not a valid value so I can do this
      [_userPreference setFloat:100.0 forKey:@"scale"]; // let default scale be 100.0
      [_userPreference synchronize];
  }

However, when I later on query this, no matter what I set in the default value, the following command:

  [self.userPreference floatForKey:@"scale"];

always return 1. I am not sure what is happening. Am I not creating the NSUserDefaults correctly?

Thank you in advance!

5
@VenkatManoharPerepa setFloat: expects a float value and not a NSString. - Amar
1.Are you modifiying Your value after getting it ?? 2. and further try to create separate NSUSERDefaults object for Retrieving and Storing let's see. in case if you using separate functions for getting and setting values if not delete your application from simulator and clean it and rebuild it. - umer sufyan
@Durgaprasad We know 100.0 is float! the previous comment to which Amar was pointing out has been deleted! - Vinayak Kini
Did you set it to 1 at some point? Once you've set it, the if clause will never evaluate to True again unless you go manually delete the user defaults for this app. - rdelmar
@rdelmar hmm that might be the case! I will investigate. - Enzo

5 Answers

3
votes

if you want to use NSUserDefaults, do not create any variables or @propertys. Just use

[[NSUserDefaults standardUserDefaults] setFloat:forKey:]
[[NSUserDefaults standardUserDefaults] synchronize];

to save your data and

[[NSUserDefaults standardUserDefaults] floatForKey:]

for fetching data.

3
votes

Store using

   [_userPreference setObject:[NSNumber numberWithFloat:100.0] forKey:@"Scale"];
   [_userPreference synchronize];

and retrieve it using

   float value = [[_userPreference objectForKey:@"Scale"] floatValue];
2
votes

Have you tried this to set the float value:

[_userPreference setObject:[NSNumber numberWithFloat:100.0f] forKey@"scale"];

and the following to retrieve:

CGFloat scale = [[_userPreference objectForKey@"scale"] floatValue];
0
votes

Change your if condition to this:

if ([_userPreference floatForKey:@"scale"] == 0.0f ) {

NSUserDefaults will always return 0.0f if no value is set for the key. Since you are saying that "also 0 is not a valid", you can safely use this approach

-1
votes

try this

 -(void) saveFloatToUserDefaults:(float)x forKey:(NSString *)key {
     NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
     [userDefaults setFloat:x forKey:key];
     [userDefaults synchronize];
 }

 -(float) retriveFromUserDefaultsForKey:(NSString *)key {
     NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
     return [userDefaults floatForKey:key];
 }

 [self saveFloatToUserDefaults:6.55 forKey:@"myFloat"];
 float x = [self retriveFromUserDefaultsForKey:@"myFloat"];