Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling Swift async function from Objective-C

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.

like image 207
Z S Avatar asked Dec 30 '25 22:12

Z S


1 Answers

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 updateStatusWithCompletionHandler: in Objective-C.

like image 50
Sulthan Avatar answered Jan 01 '26 15:01

Sulthan