Swift should include a mechanism to construct selectors from Swift method names

Originator:brent
Number:rdar://17296703 Date Originated:12-Jun-2014 07:44 PM
Status:Duplicate/16199944 Resolved:
Product:Developer Tools Product Version:Xcode6-Beta (6A215l)
Classification:Feature (New) Reproducible:Always
 
Summary:
Currently, using any selector-based API requires that the developer know how Swift methods are mapped to Objective-C selectors. Swift should not require developers to know this; instead, it should include a language feature to construct the selector for you.

Steps to Reproduce:

1. Forget everything you know about how Objective-C works.

2. Define a class like this:

    class ButtonHandler: NSObject {
        func buttonPressed(sender: AnyObject?) { println(“Pressed!” }
    }

3. Create a UIButton or NSButton and a ButtonHandler.

4. Attempt to make the ButtonHandler the button’s target in some Swifty way, for instance:

    button.addTarget(handler, action: handler.buttonPressed, forControlEvents: .TouchUpInside)

Expected Results:
This, or something like it, succeeds.

Actual Results:
It does not. You must know that the selector for the buttonPressed method is “buttonPressed:” to make this work.

Notes:
Last week, the first developers started learning to use Cocoa without knowing Objective-C. These people don’t know how selectors work and, if Swift is to be a first-class Cocoa language, shouldn’t have to, at least not beyond knowing that they represent a method name.

The “handler.buttonPressed” above is merely one example of how this feature might be implemented. Other possibilities include:

    handler.buttonPressed.selector
    ButtonHandler.buttonPressed.selector
    Selector(handler.buttonPressed)
    Selector(ButtonHandler.buttonPressed)
    @selector(buttonPressed(sender:))

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!