Created
January 20, 2017 19:41
-
-
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
This file contains hidden or 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
| //: 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