0

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;
}
Community
  • 1
  • 1
Minimi
  • 921
  • 10
  • 29
  • Possible duplicate of [Calling objective-C typedef block from swift](http://stackoverflow.com/questions/26286160/calling-objective-c-typedef-block-from-swift) – slf Oct 05 '16 at 22:20
  • I don't think it is, they're definitely similar but the answer provided didn't solve my issue. – Minimi Oct 05 '16 at 22:27
  • See my updated question – Minimi Oct 05 '16 at 22:38
  • Depends on how you declared your `replaceAll`. Does it take `[Any]?` which `leadSourceNames.flatMap({$0})` returns? – OOPer Oct 05 '16 at 22:45
  • It takes [String]! but even then the error is being thrown at the ...Client.shared().getLeadSources({ (leadSourceNames, error) in... line, it still exists even if I comment out the content. – Minimi Oct 05 '16 at 22:48
  • JUST KIDDING! I guess the error just didn't disappear after I commented out the contents but I tried compiling it after removing the replaceAll and it worked, which means that's the underlying cause. I don't understand why this caused the issue though, can you explain? @OOPer – Minimi Oct 05 '16 at 22:59

1 Answers1

1

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.

Minimi
  • 921
  • 10
  • 29