Home > OS >  Calling Swift async function from Objective-C
Calling Swift async function from Objective-C

Time:11-24

I am using the new Swift 5.5 features to create an async function:

@MainActor
@objc func updateStatus() async {
    /// do async stuff...
}

But when I try to call this method from Objective-C code, it doesn't show up in autocomplete and gives a build error if I try to build it:

if (@available(iOS 15.0, *)) {
     CJSubscriptionStatusCheck *backgroundStatusCheck = [[CJSubscriptionStatusCheck alloc] init];
     [backgroundStatusCheck updateStatus]; // No visible @interface for 'CJStatusCheck' declares the selector 'updateStatus'
}

My other swift code works fine with Objective-C so the setup is fine, but I'm not sure how the 'async' code can be used (if at all) with Objective-C. I can't find an answer to this with my research.

Thanks.

CodePudding user response:

According to SE-0297 Concurrency Interoperability with Objective-C async methods get added completionHandler parameter automatically.

Therefore the method name should be something similar to updateStatusCompletionHandler: in Objective-C.

  • Related