2017-05-26 58 views
0

AssetGridModel繼承PHAsset類並添加IsSelected屬性如何繼承PHAsset類,並添加屬性

fileprivate var fetchResult: [AssetGridModel]! 

let allPhotosOptions = PHFetchOptions() 
     allPhotosOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)] 
     let fetchAssets = PHAsset.fetchAssets(with: allPhotosOptions) 

let fetchAssets = AssetGridModel.fetchAssets(with: allPhotosOptions) 

爲什麼我已經出來的類型是PHAsset,不是我寫了繼承類,不能改變我添加的屬性?

請告訴我,謝謝

回答

0

使用運行時解決它

extension PHAsset { 

    struct RuntimeKey { 
     static var isSelectedKey = UnsafeRawPointer.init(bitPattern: "isSelectedKey".hashValue) 
    } 

    var isSelectedImage: Bool? { 
     set { 
      objc_setAssociatedObject(self, PHAsset.RuntimeKey.isSelectedKey, newValue, .OBJC_ASSOCIATION_COPY_NONATOMIC) 
     } 
     get { 
      return objc_getAssociatedObject(self, PHAsset.RuntimeKey.isSelectedKey) as? Bool 
     } 
    } 
}