Last active
September 7, 2019 00:20
-
-
Save jimmyhoran/b2d0cedc1e09191b1d96450d9f3a0a1e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
internal class ClosureSleeve { | |
let closure: () -> Void | |
init(for object: AnyObject, _ closure: @escaping () -> Void) { | |
self.closure = closure | |
objc_setAssociatedObject( | |
object, | |
String(format: "[%d]", arc4random()), | |
self, | |
objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN) | |
} | |
@objc | |
func invoke () { | |
closure() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
extension UIControl { | |
func add(for controlEvents: UIControl.Event, _ closure: @escaping () -> Void) { | |
let sleeve = ClosureSleeve(for: self, closure) | |
addTarget(sleeve, action: #selector(ClosureSleeve.invoke), for: controlEvents) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment