起初使用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一下
|
请发表评论