Search code examples
iosobjective-cnsuserdefaultsparse-error

3 parse errors obj c


I'm trying to set a switch so if the switch is on, it will make a button in a totally different view go to a different view than if the switch is off. I have done my research and found NSUserDefaults but I am getting 3 parse errors trying to enable this. My code is:

Settings View.m (Where the switch is)

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

The code trying to access the switch bool

BOOL on = [[NSUserDefaults standardUserDefaults] boolForKey:@"switchState"];

    if (on) {
        double delayInSeconds = seconds;
        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
        dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
            UIViewController *viewController4 =
            [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController4"];
            [self presentViewController:viewController4 animated:YES completion:nil];
    } else { //errors 2&3
        double delayInSeconds = seconds;
        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
        dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
            UIViewController *viewController3 =
            [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController3"];
            [self presentViewController:viewController3 animated:YES completion:nil];

    }

1:Expected expression

2:Expected ')'

3:Expected '}'

This is all the code related to this and I haven't edited any out.

Thnx


Solution

  • I don't see anything obviously wrong with the first section of code, so I can't help you with the expected expression error, but the second two errors are caused by failing to close the braces on the blocks you're passing to dispatch_after.

    For example, first section should be:

    if (on) {
        double delayInSeconds = seconds;
        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
        dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
            UIViewController *viewController4 =
            [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController4"];
            [self presentViewController:viewController4 animated:YES completion:nil];
        }); // note the brace and closing paren
    }