in my viewDidLoad method I want to read the user defaults. But
NSLog(@"%@", [prefs stringForKey:@"gateway_picker"]);
prints (null). Why?
gateway_picker default value is set to 1.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Because it is set to the number 1, as either an integer, a float, or a Boolean, and unlike an NSControl, NSUserDefaults does not attempt to convert the value to the type you requested.
Use
objectForKey:instead. This will return the object regardless of whether it is a string or anything else.If you then want to interpret it numerically, ask the object for its
integerValue,floatValue, orboolValue. This will work regardless of whether the object is an NSString or any kind of NSNumber. (If it is a data, date, array, or dictionary, this will cause an exception. You may want to detect such values and substitute the default value in that case.)If you specifically want a string, detect whether the object is a number and ask it for its
stringValueif it is. (Again, you may want to also handle receiving a data, date, array, or dictionary.)