Swift Selector Syntax Sugar
The gist of the article is that we can omit the verbose #selector
syntax by adding a convenience property:
private extension Selector {
static let buttonTapped =
#selector(ViewController.buttonTapped(_:))
}
...
button.addTarget(self, action: .buttonTapped,
forControlEvents: .TouchUpInside)
I’d make that not private
but internal
and group these in a struct to separate them from other Selector
stuff:
extension Selector {
// One for each view module ...
struct Banana {
static let changeSizeTapped =
#selector(BananaViewController.changeSizeTapped(_:))
}
struct Potato {
static let peelSelected =
#selector(PotatoViewController.peelSelected(_:))
}
}
// ...
button.addTarget(self, action: .Potato.peelSelected,
forControlEvents: .TouchUpInside)
Great tip!