Xcode spurious warnings for “Creating selector for nonexistent method 'compare:'”

余生长醉 提交于 2019-12-09 01:16:41

问题


Here's a snippet:

NSArray *a = [@[@"a", @"b", @"c"] sortedArrayUsingSelector:@selector(compare:)];

XCode (5.0) is giving me the following warning:

Creating selector for nonexistent method 'compare:'

How do I eliminate these warnings?


回答1:


This warning is relevant for you and can be disabled in the build settings of your project. Set the value to NO and the warning is disabled.

-Wselector

Warn if multiple methods of different types for the same selector are found during compilation. The check is performed on the list of methods in the final stage of compilation. Additionally, a check is performed for each selector appearing in a @selector(...) expression, and a corresponding method for that selector has been found during compilation. Because these checks scan the method table only at the end of compilation, these warnings are not produced if the final stage of compilation is not reached, for example because an error is found during compilation, or because the -fsyntax-only option is being used.


These could also be interesting:

-Wundeclared-selector

Warn if a @selector(...) expression referring to an undeclared selector is found. A selector is considered undeclared if no method with that name has been declared before the @selector(...) expression, either explicitly in an @interface or @protocol declaration, or implicitly in an @implementation section. This option always performs its checks as soon as a @selector(...) expression is found, while -Wselector only performs its checks in the final stage of compilation. This also enforces the coding style convention that methods and selectors must be declared before being used.

-Wstrict-selector-match

Warn if multiple methods with differing argument and/or return types are found for a given selector when attempting to send a message using this selector to a receiver of type id or Class. When this flag is off (which is the default behavior), the compiler will omit such warnings if any differences found are confined to types which share the same size and alignment.


You can disable the warning for only some lines of code with

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wselector"
... code which will ignore the warning
#pragma clang diagnostic pop

Or ignore it for only one file

#pragma GCC diagnostic ignored "-Wselector"

The selector is mostly dynamically created, when this error occurs. Sometimes it works for me to create a static selector:

SEL selector = NSSelectorFromString(@"compare:");
[self fireDelegateSelector:selector];

or

SEL selector = sel_registerName("compare:");
[self fireDelegateSelector:selector];

Link to documentation.



来源:https://stackoverflow.com/questions/20664918/xcode-spurious-warnings-for-creating-selector-for-nonexistent-method-compare

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!