Skip to content

Instantly share code, notes, and snippets.

@lukaskollmer
Created January 20, 2017 19:41
Show Gist options
  • Select an option

  • Save lukaskollmer/1e69bb51adb09b05d71ce776c50cc4d8 to your computer and use it in GitHub Desktop.

Select an option

Save lukaskollmer/1e69bb51adb09b05d71ce776c50cc4d8 to your computer and use it in GitHub Desktop.
Call swift init and omit parentheses because last (and only) argument is a closure
//: Playground - noun: a place where people can play
import UIKit
class LKSwitch : UISwitch {
let action: (UISwitch) -> Void
init(action: @escaping (UISwitch) -> Void) {
self.action = action
super.init(frame: .zero)
self.addTarget(self, action: #selector(didPress), for: .valueChanged)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
@objc private func didPress() {
action(self)
}
}
let mySwitch = LKSwitch { sender in
print("switch is on: \(sender.isOn)")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment