[swift-evolution] #selector and Void methods

Brent Royal-Gordon brent at architechies.com
Tue Mar 22 22:49:35 CDT 2016


So, today is day 2 of Swift 2.2 being in the hands of mere mortals. This evening, I walked into my weekly NSCoder Night meeting and talked to a pretty experienced developer struggling with this:

	class EditEventViewController: UIViewController {
		...
		@IBAction func cancel(sender: AnyObject?) {
			cancel()
		}
		func cancel() {
			// actual canceling logic is here
		}
	}
	
	class EditEventContainerViewController: UIViewController {
		...
		func prepareForSegue(_ segue: UIStoryboardSegue, sender sender: AnyObject?) {
			switch segue.identifier {
				...
				let cancelButton = UIBarButtonItem(barButtonSystemItem: .Cancel, target: nil, action: nil)
				cancelButton.target = vc
				cancelButton.action = #selector(vc.cancel)	// This is the problem line
				...
			}
		}
	}
		
`vc.cancel` was, of course, ambiguous, and the SE-0021 `vc.cancel(_:)` syntax can't select a Void function. I had to explain how to use `as Void -> Void` to select the right version.

This is merely an anecdote, but I think it may end up being an issue.

-- 
Brent Royal-Gordon
Architechies



More information about the swift-evolution mailing list