Home > Enterprise >  Where is Objective-C's [noop:] function defined?
Where is Objective-C's [noop:] function defined?

Time:11-25

In Swift, how can I test if a given selector is the noop: selector?

// Compiler error: Cannot find `noop` in score
override func doCommand(by selector: Selector) {
  if selector == #selector(noop(_:)) {
  }
}

When handling keyboard events in an AppKit application, NSResponder.doCommand(by:) will be called with a Selector. If the key chord does not map to a known action, then the selector noop: will be returned.

In Objective-C, you can test for this with @selector(noop:), however in Swift if you attempt to use #selector(noop(_:)) then the compiler complains it cannot resolve that selector. When this happens, you generally just have to prefix the selector with the class it belongs in, ex: #selector(NSResponder.moveLeft(_:)).

However, I cannot find where noop: is defined. No amount of searching through the header files reveals a suitable match.

CodePudding user response:

I can confirm that there's no headers in the entire MacOSX.sdk that define a noop: method.

Work-around: you can use NSSelectorFromString("noop:").

Of course, this circumvents the correctness-checking the compiler does for you with the more special #selector(Class.method) syntax.

  • Related