我已经将我的项目设置为能够在 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/