I am trying to store the Segment Index of a SegmentedControl in NSUserDefaults.
Here is the code for loading the preferences and the preferences ‘SAVE’ button.
- (void)viewDidLoad
{
[super viewDidLoad];
// Get the stored data before the view loads
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
int yPrefSegment = [defaults integerForKey:@"yPref"];
prefYsegmentedControl.selectedSegmentIndex = yPrefSegment;
}
- (IBAction)savePrefButton:(id)sender
{
int yPref = [[prefYsegmentedControl.selectedSegmentIndex] integerValue];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults synchronize];
NSLog(@"Data saved");
}
I am getting an error on the line (in the savePrefButton) :
int yPref = [[prefYsegmentedControl.selectedSegmentIndex] integerValue];
Error is: Expected Identifier
I am lost here. Don’t know what it is talking about.
Thank you.
You have one syntax error and two semantical errors.
The syntax error:
Choose one of the method call or dot notation for accessing properties. Use either
or
The first semantical error:
is already an integer. No need to call
integerValueon it. (You can’t send messages to primitive types anyway. This is not Smalltalk.)The second semantical error: you don’t actually store the index in
NSUserDefaults. Insertbefore you synchronize the user defaults.