Danie1s / Tiercel

简单易用、功能丰富的纯 Swift 下载框架
MIT License
2.67k stars 388 forks source link

请问如何给task 拓展一些属性 #195

Closed WilsonScott closed 1 year ago

WilsonScott commented 1 year ago

我尝试 extension DownloadTask{ var platform_type: String { get { objc_getAssociatedObject(self, &Self.TYPE_KEY) as? String ?? "" } set { objc_setAssociatedObject(self, &Self.TYPE_KEY, newValue, .OBJC_ASSOCIATION_ASSIGN) } } }

结果在sessionManager.tasks 里边拿不到每个task的platform_type

Danie1s commented 1 year ago
extension DownloadTask{
    static var TYPE_KEY: Void?

    var platform_type: String {
        get {
            objc_getAssociatedObject(self, &Self.TYPE_KEY) as? String ?? ""
        }
        set {
            objc_setAssociatedObject(self, &Self.TYPE_KEY, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }

}

你关联对象没用好