gpt4 book ai didi

ios - Swift 如何让一个函数在另一个函数完成后执行

转载 作者:搜寻专家 更新时间:2023-10-30 21:52:02 25 4
gpt4 key购买 nike

我写了两个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com