views:

79

answers:

3

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?

+2  A: 

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

Ben Gottlieb
Ditto. Perhaps the OP is confusing NSInteger with NSUInteger?
Amagrammer
If -1 is being retrieved as an unsigned int, the OP might report getting MAX_INT back.
Alex Reynolds
+1  A: 

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
A: 

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

Chrizzz