Hi all I am new to Swift and I wrote two functions (just a multiple choice quiz app)
大家好,我是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
I want in ViewDidLoad
, first execute createQuestion()
, after it is fully completed then run newQuestion()
. Otherwise the newQuestion()
has some issues when reading from local varibles that were supposed to be fetched. How am I going to manage that? Thanks
我想在ViewDidLoad中,首先执行createQuestion(),在它完全完成之后运行newQuestion()。否则,newQuestion()在读取应该获取的本地变量时存在一些问题。我该怎么做呢?谢谢
EDIT: Thanks a lot!! I learned to use closure! One more follow up question. I am using a for loop to create questions. However, the problem is that the for loop does not execute orderly. Then my check for repeated function (vocabTestedIndices) fails and it would bring two identical questions. I want the for loop to execute one by one, so the questions created will not be overlapped. thank you! code image
编辑:谢谢! !我学会了使用闭包!还有一个问题。我使用for循环来创建问题。但是,问题是for循环执行不有序。然后我对重复函数(vocabtestedindex)的检查失败了,它会带来两个相同的问题。我希望for循环逐个执行,这样创建的问题就不会被重叠。谢谢你!代码的形象
4 个解决方案
#1
7
try
试一试
override func viewDidLoad() {
super.viewDidLoad()
self.createQuestions { () -> () in
self.newQuestion()
}
}
func createQuestions(handleComplete:(()->())){
// do something
handleComplete() // call it when finished s.t what you want
}
func newQuestion(){
// do s.t
}
#2
4
What about swift defer
from this post?
斯威夫特推迟发这个帖子怎么样?
func deferExample() {
defer {
print("Leaving scope, time to cleanup!")
}
print("Performing some operation...")
}
// Prints:
// Performing some operation...
// Leaving scope, time to cleanup!
#3
0
Since you are new. I don't know if you do know closures or not so i have placed simple solution for you. (solution is similar to @Paulw11 commented on your question) just call in viewDidLoad:
因为你是新的。我不知道你是否知道闭包,所以我给你放了一个简单的解决方案。(解决方案类似于@Paulw11对您的问题的评论)只需调用viewDidLoad:
self.createQuestions()
The task you want to perform depends on the Parse response:
only after response arrives you want to call newQuestion function.
您希望执行的任务取决于解析响应:只有在响应到达之后,您才希望调用newQuestion函数。
Here is the Parse Documentation for swift: https://www.parse.com/docs/ios/guide#objects-retrieving-objects
以下是swift的解析文档:https://www.parse.com/docs/ios/guide#objects-retrieving-objects
func createQuestions() {
var query = PFQuery(className:"GameScore")
query.whereKey("playerName", equalTo:"Sean Plott")
query.findObjectsInBackgroundWithBlock {
(objects: [PFObject]?, error: NSError?) -> Void in
if error == nil {
// The find succeeded.
self.newQuestion()
} else {
// Log details of the failure
print("Error: \(error!) \(error!.userInfo)")
}
}
}
func newQuestion() {
//here is your code for new question function
}
#4
0
Closure will help you to achieve this functionality.
Create your createQuestions function as below.
闭包将帮助您实现此功能。创建createQuestions函数如下所示。
func createQuestions(completion:((Array<String>) -> ())){
//Create your local array for storing questions
var arrayOfQuestions:Array<String> = []
//Fetch questions from parse and allocate your local array.
arrayOfQuestions.append("Question1")
//Send back your questions array to completion of this closure method with the result of question array.
//You will get this questions array in your viewDidLoad method, from where you called this createQuestions closure method.
completion(arrayOfQuestions)
}
viewDidLoad
viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
//Calling createQuestions closure method.
self.createQuestions { (arrayOfQuestions) -> () in
//Pass your local questions array you received from createQuestions to newQuestion method.
self.newQuestion(arrayOfQuestions)
}
}
New Question Method
问题的新方法
func newQuestion(arrayOfQuestions:Array<String>){
//You can check your questions array here and process on it according to your requirement.
print(arrayOfQuestions)
}
#1
7
try
试一试
override func viewDidLoad() {
super.viewDidLoad()
self.createQuestions { () -> () in
self.newQuestion()
}
}
func createQuestions(handleComplete:(()->())){
// do something
handleComplete() // call it when finished s.t what you want
}
func newQuestion(){
// do s.t
}
#2
4
What about swift defer
from this post?
斯威夫特推迟发这个帖子怎么样?
func deferExample() {
defer {
print("Leaving scope, time to cleanup!")
}
print("Performing some operation...")
}
// Prints:
// Performing some operation...
// Leaving scope, time to cleanup!
#3
0
Since you are new. I don't know if you do know closures or not so i have placed simple solution for you. (solution is similar to @Paulw11 commented on your question) just call in viewDidLoad:
因为你是新的。我不知道你是否知道闭包,所以我给你放了一个简单的解决方案。(解决方案类似于@Paulw11对您的问题的评论)只需调用viewDidLoad:
self.createQuestions()
The task you want to perform depends on the Parse response:
only after response arrives you want to call newQuestion function.
您希望执行的任务取决于解析响应:只有在响应到达之后,您才希望调用newQuestion函数。
Here is the Parse Documentation for swift: https://www.parse.com/docs/ios/guide#objects-retrieving-objects
以下是swift的解析文档:https://www.parse.com/docs/ios/guide#objects-retrieving-objects
func createQuestions() {
var query = PFQuery(className:"GameScore")
query.whereKey("playerName", equalTo:"Sean Plott")
query.findObjectsInBackgroundWithBlock {
(objects: [PFObject]?, error: NSError?) -> Void in
if error == nil {
// The find succeeded.
self.newQuestion()
} else {
// Log details of the failure
print("Error: \(error!) \(error!.userInfo)")
}
}
}
func newQuestion() {
//here is your code for new question function
}
#4
0
Closure will help you to achieve this functionality.
Create your createQuestions function as below.
闭包将帮助您实现此功能。创建createQuestions函数如下所示。
func createQuestions(completion:((Array<String>) -> ())){
//Create your local array for storing questions
var arrayOfQuestions:Array<String> = []
//Fetch questions from parse and allocate your local array.
arrayOfQuestions.append("Question1")
//Send back your questions array to completion of this closure method with the result of question array.
//You will get this questions array in your viewDidLoad method, from where you called this createQuestions closure method.
completion(arrayOfQuestions)
}
viewDidLoad
viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
//Calling createQuestions closure method.
self.createQuestions { (arrayOfQuestions) -> () in
//Pass your local questions array you received from createQuestions to newQuestion method.
self.newQuestion(arrayOfQuestions)
}
}
New Question Method
问题的新方法
func newQuestion(arrayOfQuestions:Array<String>){
//You can check your questions array here and process on it according to your requirement.
print(arrayOfQuestions)
}