[swift-evolution] [Idea] Expression to retrieve the Objective-C selector of a method

Brent Royal-Gordon brent at architechies.com
Wed Dec 30 04:36:59 CST 2015


> What if selectors arguments could be imported into swift to take a closure instead ? 
> 
> This would fit into the proposal to rewrite the imported objective c Apis 
> 
> So 
> 
> - addAction:(Selector)action
> 
> Becomes
> 
> addAction(action:(AnyObject)->Void)
> 
> Instead of
> 
> addAction(action:String) 
> 
> Like it does now.

Actually, it comes in as addAction(action: Selector), not String. You can initialize a Selector from a string literal.

Three questions about your proposal:

1. Where does "AnyObject -> Void" come from? The only signature information in a selector is the (minimum) number of arguments. Those arguments can be of any type, and 

2. How are we supposed to implement this? You need to somehow convert a closure (a pointer to a bunch of captured variables with a pointer to a function embedded inside it) into a selector (a pointer to a table of selectors inside the Objective-C runtime, which does not do any normal memory management); I just don't see how you make that work. Saying "let's do this thing" doesn't mean it's *possible* to do the thing.

3. What about other uses for selectors? addAction() is all well and good, but you also need removeAction(), and Swift closures don't have stable identities to test with.

-- 
Brent Royal-Gordon
Architechies



More information about the swift-evolution mailing list