[swift-evolution] Allow Selectors to be used as Closures

Cole Kurkowski crk at fastmail.com
Fri Dec 4 13:25:47 CST 2015


I agree that a closure is a much better solution to the target-action pattern in Swift, especially since you could pass a declared method with “self.functionToCall” which is basically a type safe version of the current paradigm. All of that said, I believe the reason it doesn’t work this way right now is compatibility with objective-c and Cocoa. I’d love to see this changed, as it’s one of the least Swifty things I have to use on a regular basis. I’m just not sure exactly how large of an effort it would be to make it compatible.

Thanks for your time,
Cole Kurkowski
> On Dec 4, 2015, at 12:49, Chris Byatt <byatt.chris at gmail.com> wrote:
> 
> It feels a lot more Swift-like to me to allow a Selector argument to have the option of being a closure.
> 
> For example, instead of:
> 
> UIBarButtonItem(title: "Press", style: .Done, target: self, action: "functionToCall")
> 
> func functionToCall() {
>     // Do something
> }
> We should be able to do:
> 
> UIBarButtonItem(title: "Press", style: .Done, target: self, action: {
>     // Do Something
> })
> For simple tasks it would be a lot neater, and indeed faster to code this way.
> 
> Thanks,
> 
> Chris
>  _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151204/6453b9fb/attachment-0001.html>


More information about the swift-evolution mailing list