1:swift中闭包和oc中block一样,都是常用于回调,闭包也是一种特殊函数
2:代码演示:
import UIKitclass httpTools: NSObject { // 闭包的类型:(参数列表) -> (返回值类型) func loadData(callBack : (JsonData : String) -> ()) { dispatch_async(dispatch_get_global_queue(0,0)) { print("发送网络请求\(NSThread.currentThread())") dispatch_sync(dispatch_get_main_queue(),{ print("获取到数据,并且进行回调:\(NSThread.currentThread())") callBack(JsonData: "JsonData数据===================") }) } }}
import UIKitclass VIEwController: UIVIEwController { // 创建属性的同时初始化对象 var tools : httpTools = httpTools() overrIDe func touchesBegan(touches: Set<UItouch>,withEvent event: UIEvent?) { print("-----"); tools.loadData { (JsonData) in print("拿到了Json数据了:\(JsonData)") } }}
3:闭包的定义格式: (参数列表) -> (返回值类型)
如:callBalk : (JsonData : String) -> ()
4:swift中:标志符在前 : 类型 && OC中:类型在前 * 标志符名字
意见反馈邮件:[email protected] 欢迎你们的阅读和赞赏、谢谢!
总结以上是内存溢出为你收集整理的[swift]-闭包的简单使用、定义全部内容,希望文章能够帮你解决[swift]-闭包的简单使用、定义所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)