例如要给UIImageView扩展一个url属性
import UIKit
private var key: Void?
extension UIImageView {
@IBInspectable var url: String? {
get {
return objc_getAssociatedObject(self, &key) as? String
}
set{
objc_setAssociatedObject(self, &key,newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}
}
运用:
网友评论