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

David Smith david_smith at apple.com
Fri Dec 4 13:26:51 CST 2015


The other concern with patterns like this is that they tend to lead to reference cycles due to unintentional closure capture.

	David

> On Dec 4, 2015, at 11:25 AM, Cole Kurkowski <crk at fastmail.com> wrote:
> 
> 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 <mailto: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 <mailto:swift-evolution at swift.org>
>> https://lists.swift.org/mailman/listinfo/swift-evolution
> 
> 
> _______________________________________________
> 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/502de486/attachment.html>


More information about the swift-evolution mailing list