UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Hi all! Could someone explain this to me from Project5? Thanks!

Forums > 100 Days of Swift

We had no choice but to do that, because the handler parameter for UIAlertAction expects a method that takes itself as a parameter, and we also added a default value of “nil” so we could call it ourselves – hence the ! part. And that's what's happening here: we're giving the UIAlertAction some code to execute when it is tapped, (I kno that this code will be executed after the user tapped the button on the alert)

"and it wants to know that that code accepts a parameter of type UIAlertAction." <-> don't really get the idea from this last line,

And this is the closure:

@objc func promptForAnswer() { let ac = UIAlertController(title: "Enter answer", message: nil, preferredStyle: .alert) ac.addTextField() // Creates and alert and adds a textfield to the alert for the user to submit an answer

    // Creates the submit button of the alert on what the user will be able to tap after provided an answer
    let submitAction = UIAlertAction(title: "Submit", style: .default) { [weak self, weak ac] _ in
        guard let answer = ac?.textFields?[0].text else { return }
        self?.submit(answer)
    }

    ac.addAction(submitAction)
    present(ac, animated: true)
}

func submit(_ answer: String) {

}

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.