Search code examples
objective-cparse-error

expected expression error trying to store data with NSUserDefaults


I'm trying to use NSUserDefaults to store whether a switch is on or off in this code:

[[NSUserDefaults standardUserDefaults] setBool:switch.on forKey:@"switchState"];

But I'm getting the error expected expression on the setBool:switch.on bit (the error marker is at the beginning of switch). How do I fix this?

Also it's inside an IBAction if that helps:

- (IBAction)mathTaskSwitched:(id)sender
{
    [[NSUserDefaults standardUserDefaults] setBool:switch.on forKey:@"switchState"];
}

I haven't got any more code to do with this except for where I try and access it but I don't think that has anything to do with the error.

Thanks


Solution

  • The error is because switch is an reserved keyword. You are using that as a variable name.

    Change your variable name, that will fix the issue.

    If it is an instance variable, you can use it like self.switch.on. But better approach will be not using any reserved keyword for your variables/constants.