[英]cannot convert value of '()' to expected argument type 'String' swift 3.0
[英]Swift 3.0 cannot convert value of type (_, _)->() to Expected argument type 'ObjectsOrErrorBlock'
我已经在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
}
我调用函数的方式有什么问题? 我应该怎么称呼它?
因此,有人指出问题类似于:在非实例对象(又名BPDAPIClient)上从解决方案是实例方法的地方迅速调用Objective-C typedef块 。 shared()函数实际上返回一个实例类型的实例,因此不会在某个非实例对象上调用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})返回哪个?”
这使我指出该块的内容不正确,从而引发了错误。 这很奇怪,因为错误指向代码块的开始,而不是内容,您认为它会指出不兼容的类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.