Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing functions as parameters in Swift

I have the following function working as I expect, in iOS 8:

func showConfirmBox(msg:String, title:String,     firstBtnStr:String,     secondBtnStr:String,     caller:UIViewController) {         let userPopUp = UIAlertController(title:title,             message:msg, preferredStyle:UIAlertControllerStyle.Alert)         userPopUp.addAction(UIAlertAction(title:firstBtnStr, style:UIAlertActionStyle.Default,             handler:{action in}))         userPopUp.addAction(UIAlertAction(title:secondBtnStr, style:UIAlertActionStyle.Default,             handler:{action in}))         caller.presentViewController(userPopUp, animated: true, completion: nil) } 

I would like to make something like the following, in order to pass as arguments the methods to be executed when one or the other of the buttons are going to be touched:

func showConfirmBox(msg:String, title:String,     firstBtnStr:String, firstSelector:Selector,     secondBtnStr:String, secondSelector:Selector,     caller:UIViewController) {         let userPopUp = UIAlertController(title:title,             message:msg, preferredStyle:UIAlertControllerStyle.Alert)         userPopUp.addAction(UIAlertAction(title:firstBtnStr, style:UIAlertActionStyle.Default,             handler:{action in caller.firstSelector()}))         userPopUp.addAction(UIAlertAction(title:secondBtnStr, style:UIAlertActionStyle.Default,             handler:{action in caller.secondSelector()}))         caller.presentViewController(userPopUp, animated: true, completion: nil) } 

Obviously I am not doing the right thing with firstSelector and secondSelector, because what I have tried up to now did not work. I suppose I am not using the right syntax for what I want, but I am sure it is possible to do what I would like to do. Any idea of the way to do it properly?

like image 451
Michel Avatar asked Nov 15 '15 07:11

Michel


People also ask

Can you pass a function as a parameter?

Function CallWhen calling a function with a function parameter, the value passed must be a pointer to a function. Use the function's name (without parentheses) for this: func(print); would call func , passing the print function to it.

What is the use of passing function as parameter?

Functions are data, and therefore can be passed around just like other values. This means a function can be passed to another function as an argument. This allows the function being called to use the function argument to carry out its action.

How do you assign a variable to a function in Swift?

To assign function to a variable in Swift, declare a variable that takes specific set/type of parameters and return a specific type of value. Then we can assign a function that has same type of parameters and return value to this variable.

How is an external parameter used with a function in Swift?

The Swift Programming Language guide has the following suggestion: “The use of external parameter names can allow a function to be called in an expressive, sentence-like manner, while still providing a function body that is readable and clear in intent.”


1 Answers

Oneword answer for your question is Closures

The Default Syntax for closures is () -> ()

Instead of Selector you could directly mention the method definition

func showConfirmBox(msg:String, title:String,     firstBtnStr:String, firstSelector:(sampleParameter: String) -> returntype,     secondBtnStr:String, secondSelector:() -> returntype,     caller:UIViewController) {     //Your Code } 

But using this will create readability problems so i suggest you to use typeAlias

typealias MethodHandler1 = (sampleParameter : String)  -> Void typealias MethodHandler2 = ()  -> Void  func showConfirmBox(msg:String, title:String,                     firstBtnStr:String, firstSelector:MethodHandler1,                     secondBtnStr:String, secondSelector:MethodHandler2) {      // After any asynchronous call     // Call any of your closures based on your logic like this     firstSelector("FirstButtonString")     secondSelector() } 

You can call your method like this

func anyMethod() {    //Some other logic      showConfirmBox(msg: "msg", title: "title", firstBtnStr: "btnString",           firstSelector: { (firstSelectorString) in               print(firstSelectorString) //this prints FirstButtonString          },           secondBtnStr: "btnstring") {             //Invocation comes here after secondSelector is called           } } 
like image 193
ipraba Avatar answered Sep 19 '22 06:09

ipraba