【发布时间】:2016-10-05 21:55:58
【问题描述】:
我在 Objective-c 中使用 typedef 来定义一个完成块,如下所示:
typedef void(^ObjectsOrErrorBlock) (NSArray* objects, NSError* error);
然后我有一个将 ObjectsOrErrorBlock 作为参数的 Swift 3.0 函数。当我尝试使用该功能时,我收到标题中提到的错误。这就是我试图称呼它的方式:
BPDKAPIClient.shared().getLeadSources({ (leadSourceNames, error) in
self.replaceAll(leadSourceNames.flatMap({$0}))
})
这就是 Xcode 自动填充我的函数的方式:
BPDKAPIClient.shared().getLeadSources { ([Any]?, Error?) in
code
}
我调用函数的方式有什么问题?我应该怎么称呼它?
所以有人指出问题类似于: Calling objective-C typedef block from swift 解决方案是在非实例对象(又名 BPDAPIClient)上调用实例方法。 shared() 函数实际上返回 instancetype 的实例,因此不会在非实例对象上调用 getLeadSources 方法,而是在某个实例上调用它。这是共享的定义方式:
+ (instancetype) sharedClient;
+ (instancetype)sharedClient {
static BPDKAPIClient *sharedMyManager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedMyManager = [[self alloc] init];
// Set the client configuration to be the default.
BPDKAPIClientConfiguration* defaultConfig = [BPDKAPIClientConfiguration defaultConfiguration];
[sharedMyManager setApiClientConfig:defaultConfig];
[sharedMyManager setAppSource:@""];
});
//TODO: add logic to allow first pass at shared manager to be allowed, but subsuquent must check that we called "setAppId:ClientKey:Environment"
return sharedMyManager;
}
【问题讨论】:
-
我不认为是,它们绝对相似,但提供的答案并没有解决我的问题。
-
查看我更新的问题
-
取决于您如何声明您的
replaceAll。是否需要[Any]?leadSourceNames.flatMap({$0})返回? -
需要[String]!但即便如此,错误仍在 ...Client.shared().getLeadSources({ (leadSourceNames, error) in... 行中引发,即使我注释掉内容,它仍然存在。
标签: ios objective-c swift3 completion-block