UIButton扩展

UIButton扩展

920 发布: 2024/9/20 17:20 本文总阅读量

OC

.h

@property (nonatomic, strong) id extParam;

.m

tatic char AssociatedObjectKey;

@implementation UIButton (RKExt)


- (void)setExtParam:(id)extParam {
    objc_setAssociatedObject(self, &AssociatedObjectKey, extParam, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (id)extParam {
    return objc_getAssociatedObject(self, &AssociatedObjectKey);
}

@end

Swift

/// Btn
//import ObjectiveC
private var AssociatedObjectKey: UInt8 = 0
extension UIButton {
    var extParam: Any? {
        get {
            return objc_getAssociatedObject(self, &AssociatedObjectKey) as Any?
        }
        set(newValue) {
            objc_setAssociatedObject(self, &AssociatedObjectKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
}