在Swift中完成方法时执行一个操作

时间:2015-12-21 10:48:08

标签: ios iphone swift ipad ios9

我希望在一个方法完成时执行一个操作,我在其他方法中执行一个方法,我希望第二个方法停止,直到第一个方法完成。

我有这个方法:

func ejecutarOBJC(){
    let txtNombre = self.view.viewWithTag(4) as? UITextField
    let textoNombre=txtNombre?.text
    let txtContra = self.view.viewWithTag(5) as? UITextField
    let textoContra=txtContra?.text


    let instanceOfCustomObject: SQLViewController =  SQLViewController()
    instanceOfCustomObject.nombre = textoNombre;
    instanceOfCustomObject.contra = textoContra;
    instanceOfCustomObject.obtenerExistenciaUsuario()

}

还有另一种方法:

func otherMethod(){

    ejecutarOBJC()

//I want continue with that method when the execution of the other method finish

}

1 个答案:

答案 0 :(得分:5)

您将如何实现这一目标:

func methodOne() {

    //Method one code here

    methodTwo()

}

func methodTwo() {

    //Method Two code here.

}

根据您的评论,以下是使用异步代码时的等待方法:

func methodOne() {
    //Code goes here
    methodTwo { () -> () in
        //Method two has finished
    }
}

func methodTwo(completion: () -> ()) {
    //Code goes here
    completion()
}