Swift 参数闭包(Closure)

作者: Mcccc | 来源:发表于2016-11-08 16:09 被阅读222次

前言

swift的closure类似于oc的block
closure闭包可以方法、参数、属性等形式存在,下面主要讲述参数和属性闭包

Closure闭包

  • non-escaping Closures
closure-noescape.png

non-escaping闭包,在function return后,闭包也运行完成,
如同Closures、function在都在单线程上完成。

  • escaping Closures
closure-escape (1).png

escaping Closures闭包,在function return后,闭包的状态不一定会完成,而且闭包的调用时机、完成时间也不可控。
跟oc的block有点相似,都可异步回调!
【oc的block内存管理方面会比较特殊,swift的Closures是否也会有内存管理问题?答案是:没有,变量在Closures改变,而引用计数并没有增加】

ps:
swift1.0、2.0闭包参数都是默认escaping Closures
swift3.0之后闭包参数默认non-escaping Closures
在生命周期来说,还是non-escaping Closures比较安全,escaping Closures可做异步优化等操作

参数闭包

import UIKit

class AFN_Hepler_Z: NSObject {

    static func postDataByString(urlSting: String, bodyDic: NSDictionary!, succeed: @escaping (_ responeObject: NSString) -> (), fail: @escaping (_ error: AnyObject) -> ()){
        
    }   
}

调用

let dict_parmas:NSDictionary =  ["code" : "index",
                                         "community_id":17,
                                         "province_id":14791,
                                         "city_id":15020,
                                         "region_id":18584]
        
        AFN_Hepler_Z.postDataByString(urlSting: url_goods, bodyDic: dict_parmas, succeed: { (responeObject:NSString) in
            
            print("succeed closure")
        }) { (error:AnyObject) in
            print(error)
        }

属性闭包

在UITableViewCell中嵌套一个UICollectionView,tableViewCell添加一个属性闭包,在tableViewCell里面的CollectionView点击事件中,回调tableViewCell的属性闭包

  • step1:
    tableViewCell中书写属性闭包
var selectClosure:((NSInteger)->())? = nil

实现CollectionView代理方法,CollectionView点击回调闭包

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if (selectClosure != nil) {
            selectClosure!(indexPath.item)
        }
    }

UITableView代理方法中,设置回调操作

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  
        let cell_scene=tableView.dequeueReusableCell(withIdentifier: "SceneCell") as! SceneCell
        cell_scene.backgroundColor=UIColor.clear
        //设置回调操作
        cell_scene.selectClosure={(index:NSInteger) in
            //点击CollectionViewCell回调此方法
            print(index)
        }
        return cell_scene
    }

相关文章

  • Swift 5.3 - SE-0279 Multiple Tra

    在最初 Swift 的定义中,当方法的最后一个参数为闭包时,称该闭包为尾随闭包(trailing closure)...

  • Swift 闭包

    @Author Swift 闭包(Closure) 闭包是一种可以在代码中作为参数传递,自含的功能块。 闭包类似于...

  • Swift 参数闭包(Closure)

    前言 swift的closure类似于oc的blockclosure闭包可以方法、参数、属性等形式存在,下面主要讲...

  • Swift:尾随闭包(Trailing closure synt

    闭包(closure)在Swift中扮演了重要角色,而尾随闭包只是闭包的一种精简方式,就如闭包参数的$0和$1一样...

  • Swift5.0 - day4-闭包、属性、方法、下标

    一、闭包 1.1、闭包表达式(Closure Expression)在 Swift 里面可以通过函数 func 定...

  • 07-闭包

    闭包表达式(Closure Expression) 闭包表达式的简写 尾随闭包 示例 - 数组的排序 忽略参数 闭...

  • swift闭包学习

    闭包作为参数 参考 Swift学习之闭包

  • Swift closure闭包

    究竟什么是Closure? 说的通俗一点,一个函数加上它捕获的变量一起,才算一个closure //MARK: -...

  • swift:Closure 闭包

    @noescape @autoclosure @autoclosure(escaping)

  • Swift Closure闭包

    函数也是一种闭包 console log 如下 闭包表达式 console log 如下 闭包中的参数类型推断 c...

网友评论

    本文标题:Swift 参数闭包(Closure)

    本文链接:https://www.haomeiwen.com/subject/hhiauttx.html