【问题标题】:3 parse errors obj c3 解析错误 obj c
【发布时间】:2016-02-24 23:56:10
【问题描述】:

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

Settings 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:预期的“}”

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

谢谢

【问题讨论】:

  • switch 是关键字....不要将该名称用作变量名

标签: ios objective-c nsuserdefaults parse-error


【解决方案1】:

我看不出第一部分代码有任何明显错误,所以我无法帮助您解决预期的表达式错误,但后两个错误是由于未能关闭您所在的块上的大括号而引起的传递给 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
}

【讨论】:

  • thnx 我现在在下面的 ibaction 中收到预期的表达式错误和缺少的 @end 错误。任何想法为什么?
【解决方案2】:
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

    }

这就是问题所在。现在在你的代码中复制并替换它就可以了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-10
    • 1970-01-01
    • 1970-01-01
    • 2011-12-09
    • 1970-01-01
    相关资源
    最近更新 更多