#selector() in Swift

With closures being first class citizens in Swift and the ability of
closures to be able to capture scope, it seems a little archaic to me that
the #selector() feature exists in Swift.

For example, why does
func addTarget(_ target: Any?, action: Selector
<https://developer.apple.com/documentation/objectivec/selector>, for
controlEvents: UIControlEvents
<https://developer.apple.com/documentation/uikit/uicontrolevents>)

have a Selector type for action. Why can’t action be defined to be a
closure for example:
addTarget(_ target: Any?, action: (sender: UIControl?, forEvent
event:UIEvent?) -> Void, for controlEvents: UIControlEvents
<https://developer.apple.com/documentation/uikit/uicontrolevents>)

What do you guys think?

Cheers!

Mohit

Selectors have been around since the start of the Objective-C platform, before either macOS or iOS existed. Blocks weren't added into macOS until relatively recently (from iOS 4 https://en.wikipedia.org/wiki/Blocks_(C_language_extension) <https://en.wikipedia.org/wiki/Blocks_(C_language_extension)> if you're interested).

So many of the APIs that predated the introduction of blocks worked by having a selector that could be called back on a target, and these APIs are still present today in current releases of macOS and iOS.

The Swift based API is purely there because there are some APIs that aren't capable of taking a block, and hence aren't capable of taking a Swift closure.

Alex

···

On 10 Aug 2017, at 07:04, Mohit Athwani via swift-users <swift-users@swift.org> wrote:

With closures being first class citizens in Swift and the ability of closures to be able to capture scope, it seems a little archaic to me that the #selector() feature exists in Swift.

For example, why does
func addTarget(_ target: Any?,
        action: Selector <https://developer.apple.com/documentation/objectivec/selector>,
           for controlEvents: UIControlEvents <https://developer.apple.com/documentation/uikit/uicontrolevents>)

have a Selector type for action. Why can’t action be defined to be a closure for example:
addTarget(_ target: Any?,
        action: (sender: UIControl?, forEvent event:UIEvent?) -> Void,
           for controlEvents: UIControlEvents <https://developer.apple.com/documentation/uikit/uicontrolevents>)

What do you guys think?

Hi Alex,

Thanks for the update!

Cheers!

···

--
Mohit Athwani
http://about.me/mohitathwani

On Aug 10, 2017, 2:56 AM -0700, Alex Blewitt <alblue@apple.com>, wrote:

> On 10 Aug 2017, at 07:04, Mohit Athwani via swift-users <swift-users@swift.org> wrote:
>
> With closures being first class citizens in Swift and the ability of closures to be able to capture scope, it seems a little archaic to me that the #selector() feature exists in Swift.
>
> For example, why does
> func addTarget(_ target: Any?, action: Selector, for controlEvents: UIControlEvents)
>
> have a Selector type for action. Why can’t action be defined to be a closure for example:
> addTarget(_ target: Any?, action: (sender: UIControl?, forEvent event:UIEvent?) -> Void, for controlEvents: UIControlEvents)
>
> What do you guys think?

Selectors have been around since the start of the Objective-C platform, before either macOS or iOS existed. Blocks weren't added into macOS until relatively recently (from iOS 4 https://en.wikipedia.org/wiki/Blocks_(C_language_extension) if you're interested).

So many of the APIs that predated the introduction of blocks worked by having a selector that could be called back on a target, and these APIs are still present today in current releases of macOS and iOS.

The Swift based API is purely there because there are some APIs that aren't capable of taking a block, and hence aren't capable of taking a Swift closure.

Alex