Mar 25
After yesterday’s elaborate tutorial on how to create a Preferences UI, accessing the preferences from your code is a snap.
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; delayBeforeDialing = [userDefaults floatForKey:@"delayBeforeDialing"];
The key, “delayBeforeDialing” in this example, needs to match the Key value in Root.plist.
There are different accessor methods depending on the type of variable you want to retrieve.
- arrayForKey
- boolForKey
- dataForKey
- dictionaryForKey
- floatForKey
- integerForKey
- objectForKey
- stringArrayForKey
- stringForKey
You don’t have to use a UI for your preferences. If you just want to conveniently store values associated with your application, that you can read back later, you can use the setter methods of NSUserDefaults.
[userDefaults setFloat:delayBeforeDialing forKey:@"delayBeforeDialing"];
Read back the values as above.
September 2nd, 2008 at 13:49
It says the delayBeforeDialing is undeclared for the second line of code you provided.