OGeek|极客世界-中国程序员成长平台

标题: ios - sortedArrayUsingSelector 上的警告 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 20:07
标题: ios - sortedArrayUsingSelector 上的警告

我的 iPhone 应用中有以下代码行:

[[sections allValues] sortedArrayUsingSelectorselector(sortSectionsBySectionName];

这会生成一个未声明的选择器警告。

数组中的所有对象都实现了sortSectionsBySectionName:,所以一切都按预期进行。但是,我想摆脱警告。

有没有办法告诉编译器,对象确实会实现选择器?类型转换或类似的东西?

任何建议将不胜感激!



Best Answer-推荐答案


使用的方法应该对使用它的类公开可见。这通常意味着:

  1. sortSectionsBySectionName:添加到数组中对象的.h文件中,#import添加该 Controller 中的.h文件
  2. 在数组类中的对象上添加一个类别,在这个 Controller 的顶部,并在那里定义 sortSectionsBySectionName: 方法

一旦编译器可以在您尝试使用它的范围内看到该方法的存在,您就应该很好了。

或者,要求编译器忽略它:

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"

[[sections allValues] sortedArrayUsingSelectorselector(sortSectionsBySectionName];

#pragma clang diagnostic pop

但请注意,这(和类别方法)都可能隐藏会在运行时导致问题的问题...

关于ios - sortedArrayUsingSelector 上的警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22375979/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4