ios - How to make a closure as function parameter optional -


i have helper display alertview:

func showalertboxok(headline:string, message:string, okbuttontext:string, viewcontroller:uiviewcontroller, completionok:() -> void){      let alertcontroller = uialertcontroller(title: headline, message: message, preferredstyle:uialertcontrollerstyle.alert)      alertcontroller.addaction(uialertaction(title: okbuttontext, style: uialertactionstyle.default)         { action -> void in             if completionok() != nil {                 completionok()             }         })     viewcontroller.presentviewcontroller(alertcontroller, animated: true, completion: nil) } 

now make parameter completionokoptional. i´ve tried completionok:() -> void? = nil` gives me compiler error. calls should without parameter:

showalertboxok("could not retrieve position", "edit ios settings - geolocation denied. sorry. please fix und restart app.", "ok", self) 

and parameter this:

showalertboxok("could not retrieve position", "edit ios settings - geolocation denied. sorry. please fix und restart app.", "ok", self, { println("hello world") }) 

any help?

you use different approach , define parameter default value, this:

func showalertboxok(headline:string, message:string, okbuttontext:string, viewcontroller:uiviewcontroller, completionok:() -> void = {}){      let alertcontroller = uialertcontroller(title: headline, message: message, preferredstyle:uialertcontrollerstyle.alert)      alertcontroller.addaction(uialertaction(title: okbuttontext, style: uialertactionstyle.default)         { action -> void in             completionok()         })     viewcontroller.presentviewcontroller(alertcontroller, animated: true, completion: nil) } 

in way can call function in 2 different ways, , simplify definition of function.


Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -