贡献者:赵大财
博客:https://my.oschina.net/zhaodacai GitHub:https://github.com/dacaizhao
邮箱: dacai_zhao@163.com QQ:327532817
=============================
先附带一个OC版的 https://my.oschina.net/zhaodacai/blog/652212
class DCRuntime: NSObject {
var name:String = ""
func makeMoney() {
print(self.name,self.job ?? "","很多很多swift")
}
}
extension DCRuntime {
struct RuntimeKey {
static let job = UnsafeRawPointer.init(bitPattern: "job".hashValue)
}
var job: String? {
set {
objc_setAssociatedObject(self, DCRuntime.RuntimeKey.job, newValue, .OBJC_ASSOCIATION_COPY_NONATOMIC)
}
get {
return objc_getAssociatedObject(self, DCRuntime.RuntimeKey.job) as? String
}
}
}
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let dacai = DCRuntime()
dacai.job = "ios"
dacai.name = "zhaodacai"
dacai.makeMoney()
}
}