菜鸟教程小白 发表于 2022-12-11 17:13:52

ios - 在 Objective-C 中使用带有返回值和完成 block 的 swift 函数


                                            <p><p>我已将我的项目设置为能够在 Obj-c 类中使用 swift 文件,但由于某种原因,我无法调用具有返回类型和完成 block 的函数。</p>

<pre><code>@objc class AgentManager : NSObject {
static let instance = AgentManager()

// the sharedInstance class method can be reached from ObjC.
class func sharedInstance() -&gt; AgentManager {
    return AgentManager.instance
}

func fetchAgentInfo(agentID: String, completion:(result: Agent) -&gt;    Void) { //...networking code }
</code></pre>

<p>在objective-c文件中:</p>

<pre><code>AgentManager *manager = ;
[manager fetchAgentInfo:@&#34;string&#34; completion:^(Agent *response) {
    //No visible @interface for &#39;AgentManger&#39; declares the
    //selector fetchAgentInfo:completion:
}];
</code></pre>

<p>************解决方案******************:</p>

<p>我试图返回一个代理结构。你不能在 Objc 中使用 Swift 结构。我只是将它转换为一个类。</p>

<p>谢谢</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我认为问题出在完成的 block 签名中。请尝试以下代码:</p>

<pre><code>[self.manager fetchAgentInfo:@&#34;string&#34; completion:^(AgentManager * _Nonnull result) {

}];
</code></pre>

<p>你还有两个类 AgentManager 和 Agent,我假设这两个是同一个类,这只是一个错字。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 在 Objective-C 中使用带有返回值和完成 block 的 swift 函数,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/38860069/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/38860069/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 在 Objective-C 中使用带有返回值和完成 block 的 swift 函数