简体   繁体   中英

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:

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

I then have a Swift 3.0 function that takes the ObjectsOrErrorBlock as a parameter. 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:

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). 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. 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?"

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM