菜鸟教程小白 发表于 2022-12-13 15:41:16

ios - 协议(protocol)前向声明有什么警告?


                                            <p><p>我只是想避免警告(没有编译器抑制),但不想在我的文件中添加一些包含。这是我的代码:</p>

<pre><code>@protocol MyProto;

// ...

Protocol *p = @protocol(MyProto);
</code></pre>

<p>这是我收到的警告:</p>

<pre><code>@protocol is using a forward protocol declaration of MyProto [-Wat-protocol]
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>使用协议(protocol)文字(<code>@protocol</code>)需要在编译时链接到协议(protocol),这需要编译器查看协议(protocol)的定义。</p>

<p>我不明白您为什么不愿意“在我的文件中添加一些包含”。在使用该东西的实现文件中包含定义某些东西的头文件是很正常的。应该没有缺点。</p>

<p>如果您想在运行时从字符串中查找协议(protocol),您可以执行 <code>NSProtocolFromString(@"MyProto")</code> 之类的操作。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 协议(protocol)前向声明有什么警告?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/36257919/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/36257919/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 协议(protocol)前向声明有什么警告?