Why doesn't Xcode complain about undeclared methods when using @selector?

Why are there warnings when calling methods that are not declared in the class interface using normal means, but not when calling methods using @selector? Is this because selectors can be executed by a caller other than self?

For instance:

-(void) doStuff
{
    [self doNow]; // Warning: instance method not found
    SEL sel = @selector(doNow); // no warnings
}   

-(void) doNow {} // this method is not declared in the interface
+5
source share
4 answers

The "Undeclared Selector" warning is disabled by default. I do not know why. You can enable it in the build settings.

The documentation for these settings reads:

, "@selector (...)" , . , "@selector (...)" , @interface @protocol, @implementation. , "@selector (...)" , -Wselector . , . [GCC_WARN_UNDECLARED_SELECTOR, -Wundeclared-selector]

+14

question .

, . . . , , , .

+4

@selector , ; , - . , @class yourclassname .h, .

+2

, (.. [self doNow];), objc_msgSend, objc_msgSend_stret , struct . ( ( , ) () .) , , .

However, just by receiving a selector ( @selector(...)), it does not need to know the types. The selector is just a name, and you specified a name. It's all about what you use the selector for. If you use it in performSelector:, it also does not need to know the types, because this method only works for methods with arguments and return types of objects, so there is no ambiguity. Therefore, there is no need for warning.

+2
source

All Articles