2016-10-02 84 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 

}

相關問題