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

Question about day 32 challange.

Forums > 100 Days of Swift

So, I've finished the consolidation challange of day 32, things are finally starting to make logical sense to me.

I've finished it with some help of previously written code, and I would like some help/clarification about this specific code.

@objc func addItemAlert() {
        //Setup of alert controller
        let alertController = UIAlertController(title: "Add item", message: "Add the item that you would prefer to add", preferredStyle: .alert)
        alertController.addAction(UIAlertAction(title: "Cancel", style: .destructive))
        alertController.addTextField()

        //Is this a closure?
        let submitAction = UIAlertAction(title: "Submit", style: .default) {
            [weak self, weak alertController] action in
            guard let answer = alertController?.textFields?[0].text else { return }
            self?.submit(answer.lowercased())
        }

        alertController.addAction(submitAction)

        //Presenting controller
        present(alertController, animated: true)
    }

    func submit(_ answer: String) {
        let indexPath = IndexPath(row: 0, section: 0)
        shoppingList.insert(answer, at: 0)
        tableView.insertRows(at: [indexPath], with: .automatic)
    }

Specifically these two blocks of code:

        //Is this a closure?
        let submitAction = UIAlertAction(title: "Submit", style: .default) {
            [weak self, weak alertController] action in
            guard let answer = alertController?.textFields?[0].text else { return }
            self?.submit(answer.lowercased())
        }

        alertController.addAction(submitAction)

        //Presenting controller
        present(alertController, animated: true)
    }

    func submit(_ answer: String) {
        let indexPath = IndexPath(row: 0, section: 0)
        shoppingList.insert(answer, at: 0)
        tableView.insertRows(at: [indexPath], with: .automatic)
    }

How does 'sumbmitAction' work? does it run when submit is being called, or is it the other way around? Some clarification on this code would be appreciated thanks alot.

3      

Yes creating UIAlertAction will create closure which is then called by the alert controller if user taps on the associated button.

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.