ios - Swift: Explain grammar syntax for NSURLSession.sharedSession().dataTaskWithURL -


i'm new comer ios world. when see method downloading file. see code:

        // create url         let imageurl = nsurl(string: constants.caturl)!          // create task         let task = nsurlsession.sharedsession().datataskwithurl(imageurl) {             (data, response, error) in print("task finished")         } 

i don't understand part:

 {         (data, response, error) in print("task finished")     } 

where data response error objects come from? , type they? i'm familiar languages such java, c# , above structure strange: after method call {} codeblock. see in ios there 2 methods different above:

let task = nsurlsession.sharedsession().datataskwithurl(url: nsurl) let task = nsurlsession.sharedsession().datataskwithurl(url: nsurl, completionhandler: (nsdata?, nsurlresponse?, nserror?) -> void) 

thanks :)

the stuff in { ... } anonymous function body. stuff in (...) in names of parameters passed function.

the part of question "which type they?" question. answer swift knows declaration, cited:

let task = nsurlsession.sharedsession().datataskwithurl(url: nsurl, completionhandler: (nsdata?, nsurlresponse?, nserror?) -> void) 

so swift knows types of 3 parameters, , types can optionally omitted (and here have in fact been omitted).

you may confused omission of label completionhandler:. legal because function last parameter. in case, legal drop label , put anonymous function body outside function call parentheses (known "trailing syntax").

thus, code cited is in fact legal form implementing datataskwithurl(_:completionhandler:).


Comments

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -