I've read a few examples on declaring handlers for UIAlertViews and somehow I just can't grasp the concept of the proper syntax. I've seen a few examples where they do one of the following:
- handler: someFunction
- handler: {action in SomeFunction} (what does "action in" mean?)
- handler: { _ in print("Foo!") (again, what does "in" mean?)
My biggest concern is that I don't know what these things mean. and I'm trying to use the first style however I get the following error: "Variable used within its own initial value"
let answerVCAlert = UIAlertController(title: "Your turn", message: "What's the answer?", preferredStyle: .alert)
let submitAnswer = UIAlertAction(title: "Submit", style: .default, handler: submitAnswer(answer: " ")) //grab from textfield
let noAnswer = UIAlertAction(title: "No Answer", style: .default, handler: submitAnswer(answer: " "))
func submitAnswer(answer: String) {
print ("The string passed is \(answer)")
//compare answer to correct answer
}
func attemptAnswer() {
answerVCAlert.addAction(submitAnswer)
answerVCAlert.addAction(noAnswer)
//answerVCAlert.addTextField //how ??? too many different examples
self.present(answerVCAlert, animated: true, completion: nil)
}