7

Is there any way to pass the array "listINeed" to the handler function "handleConfirmPressed"? I was able to do this by adding it as a class variable but that seemed very hacky and now I want to do this for multiple variables so I need a better solution.

func someFunc(){
   //some stuff...
   let listINeed = [someObject]

   let alert = UIAlertController(title: "Are you sure?", message: alertMessage, preferredStyle: UIAlertControllerStyle.Alert)
   alert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))
   alert.addAction(UIAlertAction(title: "Confirm", style: .Destructive, handler: handleConfirmPressed))
   presentViewController(alert, animated: true, completion: nil)
}

func handleConfirmPressed(action: UIAlertAction){
  //need listINeed here
}
Max Crane
  • 97
  • 3
  • 7

1 Answers1

22

The easiest way is to just pass a closure to the UIAlertAction constructor:

func someFunc(){
    //some stuff...
    let listINeed = [ "myString" ]

    let alert = UIAlertController(title: "Are you sure?", message: "message", preferredStyle: UIAlertControllerStyle.Alert)
    alert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))
    alert.addAction(UIAlertAction(title: "Confirm", style: .Destructive, handler:{ action in
        // whatever else you need to do here
        print(listINeed)
    }))
    presentViewController(alert, animated: true, completion: nil)
}

If you really want to isolate the functional part of the routine, you can always just put:

handleConfirmPressedAction(action:action, needed:listINeed)

into the callback block

A slightly more obscure syntax, that would retain the feel of a function both in passing it to the completion routine and in the callback function itself would be to define handleConfirmPressed as a curried function:

func handleConfirmPressed(listINeed:[String])(alertAction:UIAlertAction) -> (){
    print("listINeed: \(listINeed)")
}

and then you can addAction using:

alert.addAction(UIAlertAction(title: "Confirm", style: .Destructive, handler: handleConfirmPressed(listINeed)))

Note that the curried function is shorthand for:

func handleConfirmPressed(listINeed:[String]) -> (alertAction:UIAlertAction) -> () {
    return { alertAction in
        print("listINeed: \(listINeed)")
    }
}
David Berry
  • 39,492
  • 12
  • 80
  • 91
  • But then how do I change this line: alert.addAction(UIAlertAction(title: "Confirm", style: .Destructive, handler: handleConfirmPressed))? – Max Crane Jan 26 '16 at 02:46
  • 1
    Just like in the example code I give. If you're unfamiliar with Swift and it's closure/block syntax, dig out the Apple Swift book and go through it. – David Berry Jan 26 '16 at 02:48
  • Ahhh I see what you mean now. I was hoping I didn't have to use a closure and I could just pass in a method, but this way works excellently. Thanks! – Max Crane Jan 26 '16 at 02:53
  • In swfit 4 It should be func handleConfirmPressed(listINeed:[String]) -> (_ alertAction:UIAlertAction) -> () { return { alertAction in print("listINeed: \(listINeed)") } } – Gopal Kohli Dec 06 '20 at 18:49