我看了两个教程。第一个关于迅速完成基本处理程序。辅导员显示以下代码示例:
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 say Hello")
}else{
println("People do not say hello")
}
})
第二个教程是关于HTTP请求的,代码如下所示:
/* 4. Make the request */
let task = session.dataTaskWithRequest(request) {data, response, downloadError in
if let error = downloadError {
println("Could not complete the request \(error)")
} else {
/* 5. Parse the data */
var parsingError: NSError? = nil
let parsedResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments, error: &parsingError) as! NSDictionary
/* 6. Use the 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 the request */
task.resume()
您可以看到第一行代码没有第一个教程那样的return void,为什么?
什么是返回无效?为什么我们必须输入return void?
使用void是可选的,可以使用带有或不带有void声明函数的方式,也可以使用或带有声明的完成处理程序
func noReturn() -> Void
等同于
func noReturn()
对于声明时的完成处理:
func isTextValid(input: String, completion: (result: Bool) -> Void)
等于
func isTextValid(input: String, completion: (result: Bool) -> ())
对于调用它的完成句柄:
(result) -> Void in
等于
(result) in
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句