views:

159

answers:

1

I have a settings bundle in my iPhone app, with the following PList:

Type: PSMultiValueSpecifier
Title: Within (Kilometres)

DefaultValue: Number: 1
Titles: Array:
    Item 1: Number: 1
    Item 2: Number: 2
Values: Array:
    Item 1: Number: 1
    Item 2: Number: 2
Key: String: km

Then in my code i have:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];   
NSInteger choiceOne = [defaults integerForKey:@"km"];

choiceOne is always 0.

What am i doing wrong?

Thanks

A: 

Have you tried actually start Settings app and select the "Within" settings, then come back to the app to check? The app won't take the default value automatically.

You can check if the key really exists with

NSNumber* obj = [defaults objectForKey:@"km"];
NSInteger choiceOne;
if (obj == nil) {
  choiceOne = 1; 
  [defaults setInteger:choiceOne forKey:@"km"];
} else {
  // check whether obj really implements -integerValue if needed.
  choiceOne = [obj integerValue];
}

Or just set the value to 1 if it's outside of the expected range.

KennyTM
The Settings app crashes when i click on the Within (kilometres) choice. Something is obviously wrong with my bundle? Although it was working a few days ago. What is the point of the default value if the app doesn't pick it up automatically?
joec