AssetGridModel 继承PHAsset类并添加isSelected属性
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,不是我写的继承类而不能改变我添加的属性?
请指教,谢谢
使用运行时解决它
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
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句