我写了两个Swift函数(只是一个选择题测验应用程序)

func createQuestions() // this goes to Parse, and fetches the questions data that are going to question the users and store them into local arrays

func newQuestion() // this also fetches some other data (for example, some incorrect choices) from Parse and read local variables and finally set the labels to correctly display to the users

我想要ViewDidLoad,首先执行createQuestion(),完全完成后再运行newQuestion()。否则,从应该被获取的局部变量读取时,newQuestion()会出现一些问题。我该如何处理?

编辑:我学会了使用闭包!还有一个后续问题。我正在使用for循环来创建问题。但是,问题在于for循环无法按顺序执行。然后,我对重复功能(vocabTestedIndices)的检查失败了,它将带来两个相同的问题。我希望for循环一个接一个地执行,因此创建的问题不会重叠。
code image

最佳答案

尝试

override func viewDidLoad() {
    super.viewDidLoad()
    self.createQuestions { () -> () in
        self.newQuestion()
    }
}


func createQuestions(handleComplete:(()->())){
    // do something
    handleComplete() // call it when finished stuff what you want
}
func newQuestion(){
    // do other stuff
}

关于ios - Swift如何让一个函数在另一个函数完成之后执行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34985108/

10-10 23:24
查看更多