1

The problem is when de selectedSegmentIndex is unselected: "UISegmentedControlNoSegment" alias "-1".

The other states (0, 1, 2 , etc.), I can store as Integers and retrieve with

carTypeSegmentedControl.selectedSegmentIndex = [defaults integerForKey:@"typeOfCar"];

But -1 is no NSInteger.

I also tried to remove the Integer out of the NSUserdefaults but a request would return an "0", which is not acceptable.

So, is there another easy way?

fbrereto
  • 35,429
  • 19
  • 126
  • 178
Chrizzz
  • 11
  • 1

3 Answers3

2

-1 is an integer, it's a negative integer. You should be able to store this in NSUserDefaults.

Ben Gottlieb
  • 85,404
  • 22
  • 176
  • 172
2

You need to use NSNumber, which is an object representation of values of common numerical and boolean types, e.g. to set:

[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:-1] forKey:kDefaultCarSegmentedControlState];

To retrieve:

NSInteger defaultCarSegmentedControlSelectedIndex = [[[NSUserDefaults standardUserDefaults] objectForKey:kDefaultCarSegmentedControlState] intValue];
Alex Reynolds
  • 95,983
  • 54
  • 240
  • 345
0

You are right Ben, -1 is indead an integer. And it is stored after all. I did an NSLog on that. Stupid i didn't try that. I didn't find clear documentation on that.

So probably my problem is somewhere els.

I guess your answer works as well, Alex.

Thank you for helping

Christian Loncle
  • 1,584
  • 3
  • 20
  • 30