菜鸟教程小白 发表于 2022-12-12 20:07:43

ios - sortedArrayUsingSelector 上的警告


                                            <p><p>我的 iPhone 应用中有以下代码行:</p>

<pre><code>[ sortedArrayUsingSelector:@selector(sortSectionsBySectionName:)];
</code></pre>

<p>这会生成一个<code>未声明的选择器</code>警告。</p>

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

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

<p>任何建议将不胜感激! </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>使用的方法应该对使用它的类公开可见。这通常意味着:</p>

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

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

<p>或者,要求编译器忽略它:</p>

<pre><code>#pragma clang diagnostic push
#pragma clang diagnostic ignored &#34;-Wundeclared-selector&#34;

[ sortedArrayUsingSelector:@selector(sortSectionsBySectionName:)];

#pragma clang diagnostic pop
</code></pre>

<p>但请注意,这(和类别方法)都可能隐藏会在运行时导致问题的问题...</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - sortedArrayUsingSelector 上的警告,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/22375979/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/22375979/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - sortedArrayUsingSelector 上的警告