Swift 统计项目中所有按钮的点击次数
class Swizzle: NSObject { override class func load() { UIButton.xxx_swizzleSendAction() } } extension UIButton { class func xxx_swizzleSendAction() { struct xxx_swizzleToken { static var onceToken : dispatch_once_t = 0 } dispatch_once(&xxx_swizzleToken.onceToken, { () -> Void in let cls : AnyClass! = UIButton.self let originalSelector = Selector("sendAction:to:forEvent:") let swizzledSelector = Selector("xxx_sendAction:to:forEvent:") let originalMethod = class_getInstanceMethod(cls, originalSelector) let swizzledMethod = class_getInstanceMethod(cls, swizzledSelector) method_exchangeImplementations(originalMethod, swizzledMethod) }) } public func xxx_sendAction(action : Selector, to : AnyObject!, forEvent : UIEvent!) { struct xxx_buttonTapCount { static var count : Int = 0 } xxx_buttonTapCount.count += 1 println(xxx_buttonTapCount.count) xxx_sendAction(action, to: to, forEvent: forEvent) } }