我在objective-c中使用了typedef来定义一个完成块,如下所示:
typedef void(^ObjectsOrErrorBlock) (NSArray* objects, NSError* error);
然后我有一个 Swift 3.0 函数,它将 ObjectsOrErrorBlock 作为参数。当我尝试使用该功能时,我收到标题中提到的错误。这就是我试图调用它的方式:
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 where the solution was an instance method is called on an non-instance object (aka 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;
}