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 completionok
optional. 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
Post a Comment