ios - Swift: How to pass in a closure as a function argument -
i'm trying figure out syntax passing in closure (completion handler) argument function.
my 2 functions are:
response handler:func responsehandler(response: nsurlresponse!, data: nsdata!, error: nserror!) -> void { var err: nserror var jsonresult: nsdictionary = nsjsonserialization.jsonobjectwithdata(data, options: nsjsonreadingoptions.mutablecontainers, error: nil) nsdictionary println("assynchronous\(jsonresult)") } query function public func queryallflightswithclosure( ) { querytype = .allflightsquery allow urlpath = "/api/v1/flightplan/" allow urlstring : string = "http://\(self.host):\(self.port)\(urlpath)" var url : nsurl = nsurl(string: urlstring)! var request : nsurlrequest = nsurlrequest(url: url) nsurlconnection.sendasynchronousrequest(request, queue: nsoperationqueue(), completionhandler:responsehandler) } i'd modify query like:
public fund queryallflightswithclosure( <closure>) { so can externally pass closure function. know there back upwards training closures i"m not sure if thats way go either. can't seem syntax correct...
i've tried:
public func queryallflightswithclosure(completionhandler : {(response: nsurlresponse!, data: nsdata!, error: nserror!) -> void} ) { but keeps giving me error
it might help defining type alias closure:
public typealias myclosure = (response: nsurlresponse!, data: nsdata!, error: nserror!) -> void that makes function signature "lighter" , more readable:
public func queryallflightswithclosure(completionhandler : myclosure ) { } however, replace myclosure aliasing, , have right syntax:
public func queryallflightswithclosure(completionhandler : (response: nsurlresponse!, data: nsdata!, error: nserror!) -> void ) { } ios xcode swift
No comments:
Post a Comment