swifty json - What is this success method? (Swift Closure) -
so copy pasted code ray wenderlich's swifty json tutorial , haven't been able understand of calls making.
i've scanned through swiftyjson library, looked @ nsurl description on developer site , checked out swift guide either can't find or bunch of miscellania back.
what these success calls mean?
func getindexwithsuccess(success: ((indexdata: nsdata!) -> void)) { loaddatafromurl(nsurl(string: url)!, completion:{(data, error) -> void in if let urldata = data { /* here */ success(indexdata: urldata) } }) } func loaddatafromurl(url: nsurl, completion:(data: nsdata?, error: nserror?) -> void) { var session = nsurlsession.sharedsession() let loaddatatask = session.datataskwithurl(url, completionhandler: { (data: nsdata!, response: nsurlresponse!, error: nserror!) -> void in if let responseerror = error { completion(data: nil, error: responseerror) } else if let httpresponse = response as? nshttpurlresponse { if httpresponse.statuscode != 200 { var statuserror = nserror(domain:"com.raywenderlich", code:httpresponse.statuscode, userinfo:[nslocalizeddescriptionkey : "http status code has unexpected value."]) completion(data: nil, error: statuserror) } else { completion(data: data, error: nil) } } }) loaddatatask.resume() } }
notice says:
func getindexwithsuccess(success: ((indexdata: nsdata!) -> void)) {
that means success parameter. follows after colon closure.
success
, therefore closure.
now, how should interpret closure?
note, above closure includes parentheses clarity.
you can leave shorten above removing them:
func getindexwithsuccess(success: (indexdata: nsdata!) -> void) {
more helpful links:
http://fuckingclosuresyntax.com
http://fuckingswiftblocksyntax.com
edit:
perhaps clearest explanation in context rearranging this:
func getindexwithsuccess( success: ( data: [doctormodel] )->( void ), moreinfo: string = "this parameter, trailing closure technique wont work" ) {
the above complies. moreinfo parameter true. trailing closures won't work. rearranging parameters closure last, can use trailing closure technique.
Comments
Post a Comment