针对“为不存在的方法'compare:'创建选择器"的Xcode虚假警告. [英] Xcode spurious warnings for "Creating selector for nonexistent method 'compare:'"

查看:90
本文介绍了针对“为不存在的方法'compare:'创建选择器"的Xcode虚假警告.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个片段:

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

XCode(5.0)给我以下警告:

XCode (5.0) is giving me the following warning:

Creating selector for nonexistent method 'compare:'

如何消除这些警告?

推荐答案

此警告与您相关,可以在项目的构建设置中禁用. 将该值设置为NO并禁用警告.

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

警告在编译过程中是否为同一选择器找到了不同类型的多种方法.在编译的最后阶段,对方法列表进行检查.此外,将对出现在@selector(...)表达式中的每个选择器执行检查,并在编译期间找到该选择器的相应方法.由于这些检查仅在编译结束时扫描方法表,因此,如果未到达编译的最后阶段,则不会生成这些警告,例如,因为在编译过程中发现错误,或者因为使用了-fsyntax-only选项.

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.

这些也可能很有趣:

-声明选择器

警告是否找到引用未声明选择器的@selector(...)表达式.如果在@selector(...)表达式之前没有声明具有该名称的方法(在@interface或@protocol声明中显式或在@implementation节中隐式),则认为未声明选择器.找到@selector(...)表达式后,此选项始终执行其检查,而-Wselector仅在编译的最后阶段执行其检查.这也强制执行了编码样式约定,即必须在使用方法和选择器之前声明它们.

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.

-严格选择符匹配

在尝试使用给定选择器向idClass类型的接收器发送消息时,是否找到给定选择器的具有不同参数的多个方法和/或返回类型.禁用此标志时(这是默认行为),如果发现的任何差异仅限于共享相同大小和对齐方式的类型,则编译器将忽略此类警告.

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];

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

链接至文档

这篇关于针对“为不存在的方法'compare:'创建选择器"的Xcode虚假警告.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆