Created
July 27, 2017 23:23
-
-
Save amelnychuck/4cc03b252d2f8ebf8a8b05dade7d07e5 to your computer and use it in GitHub Desktop.
Example of why we need closures to replace the last instances of #selectors in Cocoa Touch
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
import UIKit | |
class TabBarViewController: UITabBarController { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
} | |
// MARK: Keyboard Commands | |
override internal var keyCommands: [UIKeyCommand]? { | |
guard let controllers = viewControllers else { | |
return nil | |
} | |
var commands = [UIKeyCommand]() | |
for controller in controllers { | |
if let index = tabBar.items?.index(of: controller.tabBarItem) { | |
var pressedAction: Selector? | |
switch index { | |
case 0: | |
pressedAction = #selector(TabBarViewController.pressed1) | |
case 1: | |
pressedAction = #selector(TabBarViewController.pressed2) | |
case 2: | |
pressedAction = #selector(TabBarViewController.pressed3) | |
default: | |
break | |
} | |
if let action = pressedAction { | |
let command = UIKeyCommand(input: String(describing: index + 1), modifierFlags: .command, action: action, discoverabilityTitle: controller.tabBarItem.title!) | |
commands.append(command) | |
} | |
} | |
} | |
return commands | |
} | |
@objc func pressed1() { | |
selectedIndex = 0 | |
} | |
@objc func pressed2() { | |
selectedIndex = 1 | |
} | |
@objc func pressed3() { | |
selectedIndex = 2 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Explored some additional choises; (I find syntax to be quite intriguing... pondering about writing my own...:) )
Swift; more functional
Swift; if closures were supported instead of selectors
LiteScript; Superset of ES7 JavaScript
Custom syntax; direct translation
Custom syntax; how I think I would like to write it