給PHAsset類擴(kuò)展一個(gè) sizeString 的存儲(chǔ)屬性
import Foundation
import Photos
extension PHAsset{
private struct AssociatedKeys{
static var sizeString : String?
}
var sizeString : String? {
get {
//根據(jù)關(guān)鍵字返回給定對(duì)象的值
return objc_getAssociatedObject(self, &AssociatedKeys.sizeString) as? String
}
set {
if let newValue = newValue {
//設(shè)置給定對(duì)象的值為 newValue
objc_setAssociatedObject(self, &AssociatedKeys.sizeString, newValue as String?, objc_AssociationPolicy.OBJC_ASSOCIATION_COPY_NONATOMIC)
}
}
}
}
*** objc_setAssociatedObject 的四個(gè)參數(shù):源對(duì)象鸯绿,關(guān)鍵字先煎,關(guān)聯(lián)的對(duì)象 (newValue也就是你設(shè)置的值) 和一個(gè)關(guān)聯(lián)策略(要深究的話點(diǎn)進(jìn)去看官方注釋)***
***關(guān)于 objc_AssociationPolicy竟趾,它是一個(gè)枚舉:***
OBJC_ASSOCIATION_ASSIGN 等價(jià) @property(assign)
OBJC_ASSOCIATION_RETAIN_NONATOMIC 等價(jià) @property(nonatomic,strong)
OBJC_ASSOCIATION_COPY_NONATOMIC 等價(jià) @property(nonatomic,copy)
OBJC_ASSOCIATION_RETAIN 等價(jià) @property(atomic,strong)
OBJC_ASSOCIATION_COPY 等價(jià) @property(atomic,copy)