I am trying to set an NSNumber as a float value that has been loaded using NSUserDefaults but i'm getting an error. I can't understand why...?
Here's my code:
[settingsData.sensitivitySettingValue floatValue] = [[NSUserDefaults standardUserDefaults] floatForKey:@"sensitivityKey"];
It works if I use a float on its own (i.e float myFloat = [[NSUserDefaults standardUserDefaults] floatForKey:@"sensitivityKey"]
, but I want the value available across all view controllers so I have defined it in a model class as an NSNumber.
So, please would anyone be able to advise me on why that statement wont work?
taking settingsData as an object of your datamodel class where sensitivitySettingValue is a variable declared as NSNumber using something as
[settingsData.sensitivitySettingValue floatValue]
actually fetches its value rather than setting a float value to it . You can either use the dot notation such as
settingsData.sensitivitySettingValue = [NSNumber numberWithFloat:[[NSUserDefaults standardUserDefaults] floatForKey:@"sensitivityKey"]];
or something as
[settingsData setSensitivitySettingValue:[NSNumber numberWithFloat:[[NSUserDefaults standardUserDefaults] floatForKey:@"sensitivityKey"]]];
Hope it helps
That looks like you're trying to assign a variable to a getter method, judging by your syntax. What does floatValue do? Return a value or assign one?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With