【问题标题】:'weak' may only be applied to class and class-bound protocol types, not '(ViewController) -> () -> ViewController''weak' 可能只适用于类和类绑定协议类型,而不适用于 '(ViewController) -> () -> ViewController'
【发布时间】:2021-09-27 13:09:40
【问题描述】:
import UIKit

class ViewController: UIViewController {

    var myAction: ((Int, Bool) -> Void)? = { [weak self] int, bool in

    }
}

我不知道为什么会出现此错误。我以为self应该是ViewController的一个实例,怎么变成 '(ViewController) -> () -> ViewController'了?

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    在您的属性初始化程序运行时,self 尚不可用。您需要使用lazy loading — 这很简单。只需在变量前面添加lazy

    lazy var myAction: ((Int, Bool) -> Void)? = { [weak self] int, bool in
        
    }
    

    【讨论】:

    • ftw 照常。
    猜你喜欢
    • 2016-10-26
    • 2020-04-23
    • 1970-01-01
    • 1970-01-01
    • 2016-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多