Swift一些概念定义 | 三

一,创建闭包属性,赋值并且执行,使其弱引用:

    var flag: Bool = false;
    //定义
    var operate: ((String) -> String)? ;
    //
    override func viewDidLoad() {
        super.viewDidLoad()
        //赋值
        self.operate = {
            [weak self]
            (text: String) -> String in
            return "\(text) + \(self!.flag)";
        };
        //执行
        let op = operate!("this is block");
        print(op);
    }

一般应用于:
var operate_q: (() -> Void)?;
self.dismiss(animated: true, completion: self.operate_q);

Leave a Reply

Your email address will not be published. Required fields are marked *