Skip to content

Instantly share code, notes, and snippets.

@dbrockman
Created February 22, 2017 10:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dbrockman/385acfbf8c963919f8ef19aeaed0d953 to your computer and use it in GitHub Desktop.
Save dbrockman/385acfbf8c963919f8ef19aeaed0d953 to your computer and use it in GitHub Desktop.
//
// TargetClosure is a bridge from closures to objc targets/selectors
// Create a TargetClosure with a closure and pass it as the target, then pass target.selector as the selector.
//
// Example:
//
// let target = TargetClosure { doYourThing() }
// oldSchoolFunction(target: target, action: target.selector)
//
public class TargetClosure {
public let selector: Selector
private let action: () -> Void
init(_ closure: @escaping () -> Void) {
selector = #selector(run)
action = closure
}
@objc public func run() {
action()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment