Why sometime we use -> void for completion handler in swift but sometime not? -
i have watched 2 tutorial. first 1 basic completion handler in swift. tutor shows example of code:
func istextvalid(input: string, completion: (result: bool) -> void) { if (input == "hello") { completion(result: true) }else{ completion(result:false) } } istextvalid("hello", { (result) -> void in if (result == true) { println("people hello") }else{ println("people not hello") } })
the second tutorial http request code looks this:
/* 4. make request */ let task = session.datataskwithrequest(request) {data, response, downloaderror in if let error = downloaderror { println("could not complete request \(error)") } else { /* 5. parse data */ var parsingerror: nserror? = nil let parsedresult = nsjsonserialization.jsonobjectwithdata(data, options: nsjsonreadingoptions.allowfragments, error: &parsingerror) as! nsdictionary /* 6. use data! */ if let error = parsingerror { println(error) } else { if let results = parsedresult["results"] as? [[string : anyobject]] { self.movies = movie.moviesfromresults(results) dispatch_async(dispatch_get_main_queue()) { self.tableview.reloaddata() } } else { println("could not find results in \(parsedresult)") } } } } /* 7. start request */ task.resume()
you can see first line of code doesn't have return void in first tutorial, why?
what return void anyway? why have type return void?
the use of void optional same way can declare function or without void can declare completion handler or wihtout
func noreturn() -> void
is identical
func noreturn()
for completion handles when declaring it:
func istextvalid(input: string, completion: (result: bool) -> void)
is equals
func istextvalid(input: string, completion: (result: bool) -> ())
for completion handles when calling it:
(result) -> void in
is equals
(result) in
Comments
Post a Comment