3 解析错误 obj c

3 parse errors obj c

我正在尝试设置一个开关,如果开关打开,它将使处于完全不同视图的按钮转到与开关关闭时不同的视图。我已经完成研究并发现 NSUserDefaults 但我在尝试启用此功能时遇到 3 个解析错误。我的代码是:

设置View.m(开关在哪里)

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

试图访问开关布尔的代码

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:预期表达

2: 应为 ')'

3:应为“}”

这是与此相关的所有代码,我还没有编辑任何内容。

谢谢

我没有发现第一段代码有任何明显的错误,所以我无法帮助你解决预期的表达式错误,但后两个错误是由于未能关闭块上的大括号引起的正在传递给 dispatch_after。

例如,第一部分应该是:

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
}
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];
});// You didn't close your block
    } 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];
});// You didn't close your block

    }

这就是问题所在。现在将其复制并替换到您的代码中即可。