Danie1s/Tiercel

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

WilsonScott opened this issue · 1 comments

我尝试 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

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)
        }
    }
    
}

你关联对象没用好