sortSectionsBySectionName

sortSectionsBySectionName

我的iPhone应用程序中包含以下代码行:

[[sections allValues] sortedArrayUsingSelector:@selector(sortSectionsBySectionName:)];

会产生Undeclared selector警告。

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

有什么办法告诉编译器,这些对象确实会实现选择器?铸造或类似的东西?

任何建议,将不胜感激!

最佳答案

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

  • 在数组中对象的.h文件中添加sortSectionsBySectionName:,在此控制器中将.h文件添加#import
  • 在此控制器的顶部,在数组类中的对象上添加一个类别,并在其中定义sortSectionsBySectionName:方法

  • 一旦编译器在您要使用的范围内看到该方法的存在,您就应该很好。

    或者,要求编译器忽略它:
    #pragma clang diagnostic push
    #pragma clang diagnostic ignored "-Wundeclared-selector"
    
    [[sections allValues] sortedArrayUsingSelector:@selector(sortSectionsBySectionName:)];
    
    #pragma clang diagnostic pop
    

    但请注意,这种方法(以及类别方法)都可能隐藏会在运行时引起问题的问题...

    09-26 16:17