Extension Class association Object

class Person { }

extension Person {
    private static var AGE_KEY: Void?
    private static var WEIGHT_KEY: Void?
    
    var age: Int {
        get {
            objc_getAssociatedObject(self, &Person.AGE_KEY) as! Int
        }
        set {
            objc_setAssociatedObject(self, &Self.AGE_KEY, newValue, .OBJC_ASSOCIATION_ASSIGN)
        }
    }
}

你可能感兴趣的