I was adding a target to a UIButton for my letterTapped()
function like this:
button.addTarget(self, action: "letterTapped:", forControlEvents: .TouchUpInside)
I like how .TouchUpInside
works with autocompletion and it looks neater and seems safer than the string I'm supposed to use for the action:
parameter. So I searched and found this tutorial which uses enums to replace "magic strings".
I created an enum like this:
enum functionForAction: Selector {
case clearTapped = "clearTapped:"
case submitTapped = "submitTapped:"
case letterTapped = "letterTapped:"
}
Then I use it like this:
button.addTarget(self, action: functionForAction.letterTapped.rawValue, forControlEvents: .TouchUpInside)
I get code completion and I'm never trying to call a misspelled selector. Feels better. But is it possible to improve it? I'd really just like to type this:
button.addTarget(self, action: .letterTapped, forControlEvents: .TouchUpInside)
Can enums make this happen for me in Swift?