0

Quite a stupid problem really, but I am very new to the world of programming an have run into this problem where I am trying to pass a float variable from one view controller to another. I have found this great way in which to do it with strings, but I cannot get it to work with floats. Any suggestions?

Your help is much appreciated!

//Passing the data in first view controller
float savedAudioLevel = _slAudioVolume.value;
NSUserDefaults *defaultsAudio = [NSUserDefaults standardUserDefaults];
[defaultsAudio setFloat:savedAudioLevel forKey:@"currentAudioLevel"];
[defaultsAudio synchronize];

//Receiving the data in second
NSUserDefaults *defaultsAudio = [NSUserDefaults standardUserDefaults];
float loadfloatAudio = [defaultsAudio valueForKey:@"currentAudioLevel"]; //Getting an error here
_currentAudioVolume = loadfloatAudio;
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
mort
  • 71
  • 1
  • 6

1 Answers1

0

You might have an easier time if you embed your "float" into a "NSNumber" object, which is a native Objective C object you can more easily pass around.

"valueForKey", at least when called on a "NSDictionary" object, only works for NSStrings when doing KVC.

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215