[swift-evolution] Selector for current method

Timothy J. Wood tjw at omnigroup.com
Tue Nov 15 11:45:35 CST 2016


> On Nov 14, 2016, at 11:07 AM, Jordan Rose via swift-evolution <swift-evolution at swift.org> wrote:
> 
> This doesn’t seem unreasonable, but I’m not sure if that makes it reasonable. :-) What’s your use case? The stripped-down code seems like it could use any unique key, including #function.
> 
> Jordan


One use case for bare `#selector` would be in patterns like NSTextViewDelegate’s -textView:doCommandBySelector:, where there are many call sites that need to pass the selector (and thus are open to copy-paste errors). I could imagine having a helper to make this less error prone and less verbose:

class SomeView : NSView {
	private func validate(selector: Selector = #selector, action: (Void) -> Void) -> Bool {
		guard checkDelegate(selector) else {
			return
		}
		action()
	}

	@objc func action1(_ sender: AnyObject?) {
		validate {
		    // do action
		}
	}

	...

	@objc func actionN(_ sender: AnyObject?) {
		validate {
		    // do action
		}
	}

}

-tim



More information about the swift-evolution mailing list