简体   繁体   English

Swift 3.0无法将(_,_)->()类型的值转换为期望的参数类型'ObjectsOrErrorBlock'

[英]Swift 3.0 cannot convert value of type (_, _)->() to Expected argument type 'ObjectsOrErrorBlock'

I've used typedef in objective-c to define a completion block like so: 我已经在Objective-C中使用typedef来定义一个完成块,如下所示:

typedef void(^ObjectsOrErrorBlock) (NSArray* objects, NSError* error);

I then have a Swift 3.0 function that takes the ObjectsOrErrorBlock as a parameter. 然后,我有了一个Swift 3.0函数,该函数将ObjectsOrErrorBlock作为参数。 When I try to use the function I receive the error mentioned in the title. 当我尝试使用该功能时,我收到标题中提到的错误。 This is how I'm attempting to call it: 这就是我试图称呼它的方式:

BPDKAPIClient.shared().getLeadSources({ (leadSourceNames, error) in

    self.replaceAll(leadSourceNames.flatMap({$0}))
})

This is how Xcode autofills my function: 这是Xcode自动填充我的函数的方式:

BPDKAPIClient.shared().getLeadSources { ([Any]?, Error?) in
    code
}

What's wrong with the way I'm calling the function? 我调用函数的方式有什么问题? How should I be calling it? 我应该怎么称呼它?

So it was pointed out that the question is similar to: Calling objective-C typedef block from swift where the solution was an instance method is being called on a non-instance object (aka BPDAPIClient). 因此,有人指出问题类似于:在非实例对象(又名BPDAPIClient)上解决方案是实例方法的地方迅速调用Objective-C typedef块 The shared() function actually returns an instance of instancetype so the getLeadSources method isnt being called on a non-instance object it's being called on some instance. shared()函数实际上返回一个实例类型的实例,因此不会在某个非实例对象上调用getLeadSources方法。 This is how shared is defined: 这是共享的定义方式:

+ (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;
}

So from the comments, 所以从评论中

"Depends on how you declared your replaceAll. Does it take [Any]? which leadSourceNames.flatMap({$0}) returns?" “取决于您声明replaceAll的方式。是否需要[Any]?LeadSourceNames.flatMap({$ 0})返回哪个?”

which pointed me to the content of the block being incorrect causing the error to be thrown. 这使我指出该块的内容不正确,从而引发了错误。 It's weird because the error points to the start of the block, not the content, you'd think it would say incompatible types. 这很奇怪,因为错误指向代码块的开始,而不是内容,您认为它会指出不兼容的类型。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将'()'的值转换为预期的参数类型'String'swift 3.0 - cannot convert value of '()' to expected argument type 'String' swift 3.0 Swift 1.2到Swift 2:无法将类型的值转换为预期的参数类型 - Swift 1.2 to swift 2: Cannot convert value of type to expected argument type swift无法将类型(_,_)-> _的值转换为预期的参数类型'((_,CGFloat))-> _ - swift cannot convert value of type (_, _) -> _ to expected argument type '((_, CGFloat)) -> _ 无法将“字符串”类型的值转换为预期的参数类型“ NSManagedObject” Swift - Cannot convert value of type 'String' to expected argument type 'NSManagedObject' Swift swift:无法将类型“()”的值转换为预期的参数类型“ [Double]” - swift : Cannot convert value of type '()' to expected argument type '[Double]' Swift 无法将类型“()”的值转换为预期的参数类型“() -> Void” - Swift Cannot convert value of type '()' to expected argument type '() -> Void' ios无法将类型“()”的值转换为预期的参数类型“字符串”,迅速3 - ios Cannot convert value of type '()' to expected argument type 'String' swift 3 无法在Swift 3中将字符串类型的值转换为预期的参数类型int - cannot convert value of type string to expected argument type int in swift 3 Swift类别:“无法将类型的值转换为预期的参数类型'AnyObject!' - Swift category: "Cannot convert value of type to expected argument type 'AnyObject!' Swift:无法将类型'() - > Bool'的值转换为预期的参数类型'PFObject' - Swift: Cannot convert value of type '() -> Bool' to expected argument type 'PFObject'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM