skip to Main Content

I am new to this escape function in Swift, but I follow a tutorial and I use the following function below: (the function is working for me)

static func showThreeOptions(messageText: String, titleOne:String, titleTwo: String, actionOne: @escaping () -> (Void), actionTwo: @escaping () -> (), currentView: UIViewController  ) {
    
    // create the alert
    let alert = UIAlertController(title: "Alerta", message: messageText, preferredStyle: UIAlertController.Style.alert)
    // add the actions (buttons)
    alert.addAction(UIAlertAction(title: titleOne, style: UIAlertAction.Style.default, handler: { (alert) in
        actionOne()
    } ))
    alert.addAction(UIAlertAction(title: titleTwo, style: UIAlertAction.Style.default, handler: { (alert) in
        actionTwo()
    } ))
    alert.addAction(UIAlertAction(title: "Cancelar", style: UIAlertAction.Style.destructive, handler: nil))
    // show the alert
    currentView.present(alert, animated: true, completion: nil)
}

Now, I want to change the actionTwo() to actionTwo(number:Int),

but I don’t know how to change the signature actionTwo: @escaping () -> ()

How can I change the signature
actionTwo: @escaping () -> () to allow to be able to call actionTwo(number:Int) ?

—–UPDATE—–

I create the function
actionTwo(2) and it works. Thank you @RobNapier

But there is another problem now.

I call the function

AlertActions.showThreeOptions(
        messageText: "Resenha Finalizada.",
        titleOne: "Marcas/Fotos",
        titleTwo: "Editar",
        actionOne: self.someHandlerOne,
        actionTwo: self.someHandlerTwo(2),
        currentView: self
    )

This is the functions

func someHandlerOne() {
    print("test")
}

func someHandlerTwo(_ id:Int) {
    print("test2")
}

Now I get the following error when I call someHandlerTwo(_ id:Int)

Cannot convert value of type ‘()’ to expected argument type ‘(Int) -> ()’

How can I fix that error?

—–UPDATE 2—–

I find out how to use a escaping function now

func notImplemented(resDado_id: Int) -> () {
    print(resDado_id)
}

2

Answers


  1. Change @escaping () -> () to @escaping (Int) -> (). Instead of something that takes no parameters, you want something that takes one.

    It’s a little nicer to use Void for return values that are (), like (Int) -> Void, but it means the same thing.

    Login or Signup to reply.
  2. My suggestion is a different approach:

    Write an extension of UIViewController and use the UIAlertAction handler signature for actionOne and actionTwo.

    This is still more versatile and the UIAlertAction handler closures don’t escape

    extension UIViewController {
        func showThreeOptions(messageText: String, titleOne:String, titleTwo: String, actionOne: ((UIAlertAction) -> Void)? = nil, actionTwo: ((UIAlertAction) -> Void)? = nil) {
            
            // create the alert
            let alert = UIAlertController(title: "Alerta", message: messageText, preferredStyle: .alert)
            // add the actions (buttons)
            alert.addAction(UIAlertAction(title: titleOne, style: .default, handler: actionOne))
            alert.addAction(UIAlertAction(title: titleTwo, style: .default, handler: actionTwo))
            alert.addAction(UIAlertAction(title: "Cancelar", style: .destructive, handler: nil))
            // show the alert
            self.present(alert, animated: true, completion: nil)
        }
    }
    

    The closures can even be declared as functions for example

    func actionOne(action : UIAlertAction) {
        //
    }
    

    Edit:

    You don’t need to pass a parameter, you can create the handler inline and capture the id

    func deleteSomething(at id: Int) {
        let handler : (UIAlertAction) -> Void = { action in
            db.deletarResDados(id: id)
        }
        showThreeOptions(messageText: "Resenha Finalizada.",
                         titleOne: "Marcas/Fotos",
                         titleTwo: "Editar", 
                         actionOne: nil, 
                         actionTwo: handler)
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search