简体   繁体   中英

Using swift function with return value and completion block in Objective-C

I've set up my project to be able to use swift files in Obj-c classes but for some reason I cant call functions that have return types and completion blocks.

@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 }

in the objective-c file:

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

************Solution*************:

I was trying to return an Agent struct. You cant use Swift structs in Objc. I just converted it to a class.

thanks

I believe the problem is in completion's block signature. Please try this code:

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

}];

Also you have two classes AgentManager and Agent, I assumed that these two are the same class and it was just a typo.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM