首页 > 随笔档案 > Swift 3 使用objc_setAssociatedObject

Swift 3 使用objc_setAssociatedObject

Publish:

起初使用runtime添加属性的时候照着OC习惯

objc_setAssociatedObject(self, "sharedInstance","sharedInstance",.OBJC_ASSOCIATION_RETAIN_NONATOMIC)print(objc_getAssociatedObject(self, "sharedInstance"))

结果无论如何都添加不成功, 后来参照此文

let key: UnsafeRawPointer! = UnsafeRawPointer.init(bitPattern: "sharedInstance".hashValue)objc_setAssociatedObject(self,key, "sharedInstance", .OBJC_ASSOCIATION_RETAIN_NONATOMIC)print(objc_getAssociatedObject(self, key))

成功,mark一下

声明: 本文采用 BY-NC-SA 授权。转载请注明转自: levy