[swift-evolution] Type-safe selectors

Michel Fortin michel.fortin at michelf.ca
Sat Dec 5 06:30:02 CST 2015

Le 4 déc. 2015 à 21:49, Brent Royal-Gordon <brent at architechies.com> a écrit :

> 	func addTarget<T: NSObject>(target: T?, action: @convention(selector) T -> AnyObject -> Void, forControlEvents: UIControlEvents)

Ok, just realized there's a problem here. This is a valid action method:

	func someAction(sender: UIButton)

It won't work with the above addTarget because it expects a UIButton instead of AnyObject. The signature should say "Self" in the selector type instead of AnyObject so that you can only pass it to addTarget if the control is a UIButton... But you can't use Self in this context, so it still doesn't work.

Michel Fortin
michel.fortin at michelf.ca

More information about the swift-evolution mailing list