2016-10-02 58 views
0

在像C或Java,其他语言,如果我要停止执行的方法和返回值我喜欢下面:如何停止在Swift中执行某个操作?

public void aFunction() { 
    if(a == b) { 
    // Do something... 
    return; // stop here 
    } 
    // Do something else 
} 

如果迅速,我指定动作的按钮

@IBAction func button_1(sender: AnyObject) { 
    if a == b 
    { 
     // Do something ... 
     /* I want to stop here but I don't know how */ 
    } 
    // Do something else 
} 

如何停止在Action中执行脚本? 谢谢。

回答

3

不知道你遇到了什么问题...你可以使用return就好了。

func foo(a: Int, b: Int) { 
    if a == b 
    { 
     print("same") 
     return 
    } 
    print("different") 
} 

foo(a: 3, b: 3) 
foo(a: 5, b: 3) 

按预期工作。

0

您可以像后卫statment e.g一定条件下使用return语句从函数范围退出:

@IBAction func SendAction(sender: UIButton) { 
guard a != b else { 
    // Do something 
    return 
} 
// Do something else 

}

相关问题