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

标签 ios objective-c swift

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

@objc class AgentManager : NSObject {
static let instance = AgentManager()

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

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

在 objective-c 文件中:

AgentManager *manager = [AgentManager sharedInstance];
[manager fetchAgentInfo:@"string" completion:^(Agent *response) {
    //No visible @interface for 'AgentManger' declares the
    //selector fetchAgentInfo:completion:
}];

************解决方案***************:

我试图返回一个 Agent 结构。你不能在 Objc 中使用 Swift 结构。我只是将它转换为一个类。

谢谢

最佳答案

我认为问题出在完成的 block 签名中。请试试这段代码:

[self.manager fetchAgentInfo:@"string" completion:^(AgentManager * _Nonnull result) {

}];

还有两个类 AgentManager 和 Agent,我假设这两个是同一个类,这只是一个错字。

关于ios - 在 Objective-C 中使用带有返回值和完成 block 的 swift 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38860069/

相关文章:

ios - 运行程序时出现未知错误

swift - SKView 警告日志

ios - 是否可以在 draw(_ rect : CGRect) function in a UIView subclass that is backed by a CATiledLayer? 中在矩形边界外画一条线

ios - 更改UIToolbar UIBarMetrics

iphone - 验收后的图标光泽度

objective-c - 泄漏是如何发生的?

ios - 将 Storyboard 元素关联到类中已定义的导出和操作

swift - Vapor 4 与 Heroku 的预定工作

iphone - lib commonCrypto 不适用于 iOS 模拟器?

iphone - 随机选择UIView