//
//  Stevia+Tap.swift
//  LoginStevia
//
//  Created by Sacha Durand Saint Omer on 01/10/15.
//  Copyright © 2015 Sacha Durand Saint Omer. All rights reserved.
//

import UIKit

typealias ActionBlock = (() -> Void)?

class ClosureWrapper {
    var closure:ActionBlock
    
    init(_ closure:ActionBlock) {
        self.closure = closure
    }
}

private var kButtonBlockAssociationKey: UInt8 = 0
public extension UIButton {
    
    internal var testButtonBlock:ActionBlock {
        get {
            if let cw = objc_getAssociatedObject(self, &kButtonBlockAssociationKey) as? ClosureWrapper {
                return cw.closure
            }
            return nil
        }
        set(newValue) {
            objc_setAssociatedObject(self, &kButtonBlockAssociationKey, ClosureWrapper(newValue), objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
    
    public func tap(block:() -> Void) -> UIButton {
        #if swift(>=2.2)
        addTarget(self, action: #selector(UIButton.tapped), forControlEvents: .TouchUpInside)
        #else
        addTarget(self, action: "tapped", forControlEvents: .TouchUpInside)
        #endif
        testButtonBlock = block
        return self
    }
    
    func tapped() {
        testButtonBlock?()
    }
}
//
//  Stevia+Tap.swift
//  LoginStevia
//
//  Created by Sacha Durand Saint Omer on 01/10/15.
//  Copyright © 2015 Sacha Durand Saint Omer. All rights reserved.
//

import UIKit

typealias ActionBlock = (() -> Void)?

class ClosureWrapper {
    var closure:ActionBlock
    
    init(_ closure:ActionBlock) {
        self.closure = closure
    }
}

private var kButtonBlockAssociationKey: UInt8 = 0
public extension UIButton {
    
    internal var testButtonBlock:ActionBlock {
        get {
            if let cw = objc_getAssociatedObject(self, &kButtonBlockAssociationKey) as? ClosureWrapper {
                return cw.closure
            }
            return nil
        }
        set(newValue) {
            objc_setAssociatedObject(self, &kButtonBlockAssociationKey, ClosureWrapper(newValue), objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
    
    public func tap(block:() -> Void) -> UIButton {
        addTarget(self, action: "tapped", forControlEvents: .TouchUpInside)
        testButtonBlock = block
        return self
    }
    
    func tapped() {
        testButtonBlock?()
    }
}
--- 
+++ 
@@ -34,7 +34,11 @@
     }
     
     public func tap(block:() -> Void) -> UIButton {
+        #if swift(>=2.2)
+        addTarget(self, action: #selector(UIButton.tapped), forControlEvents: .TouchUpInside)
+        #else
         addTarget(self, action: "tapped", forControlEvents: .TouchUpInside)
+        #endif
         testButtonBlock = block
         return self
     }
FIx Swift 2.2  selector warning
