【问题标题】:Using swift function with return value and completion block in Objective-C在 Objective-C 中使用带有返回值和完成块的 swift 函数
【发布时间】:2016-08-09 20:42:38
【问题描述】:

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

@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:
}];

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

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

谢谢

【问题讨论】:

    标签: ios objective-c swift


    【解决方案1】:

    我认为问题出在完成的块签名中。请尝试以下代码:

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

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

    【讨论】:

    • 我实际上返回了一个代理结构
    • 这解释了什么是错的,遗憾的是你不能在objective-c中使用swift结构。您可以尝试改用类。
    猜你喜欢
    • 1970-01-01
    • 2017-05-12
    • 1970-01-01
    • 2016-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-13
    相关资源
    最近更新 更多