Ios 3解析错误obj c

Ios 3解析错误obj c,ios,objective-c,nsuserdefaults,parse-error,Ios,Objective C,Nsuserdefaults,Parse Error,我试图设置一个开关,这样如果开关打开,它会使一个完全不同视图中的按钮转到与开关关闭时不同的视图。我做了研究,发现了NSUserDefaults,但我在尝试启用此功能时遇到了3个解析错误。我的代码是: 设置视图.m(开关所在位置) 试图访问开关布尔的代码 BOOL on = [[NSUserDefaults standardUserDefaults] boolForKey:@"switchState"]; if (on) { double delayInSeconds =

我试图设置一个开关,这样如果开关打开,它会使一个完全不同视图中的按钮转到与开关关闭时不同的视图。我做了研究,发现了
NSUserDefaults
,但我在尝试启用此功能时遇到了3个解析错误。我的代码是:

设置视图.m(开关所在位置)

试图访问开关布尔的代码

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:应为“}”

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


Thnx

我看不出第一段代码有任何明显的错误,因此我无法帮助您解决预期的表达式错误,但后两个错误是由于未能关闭要传递给dispatch\u的块上的大括号而导致的

例如,第一节应为:

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
}

这就是问题所在。现在在您的代码中复制并替换它,它会工作。

thnx我现在在下面的iAction中得到一个预期的表达式错误和一个缺少的@end错误。知道为什么吗?
switch
是一个关键字……不要将该名称用作变量名
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

    }