Calling Swift async function from Objective-C
Asked Answered
S

1

14

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.

Seafaring answered 23/11, 2021 at 18:30 Comment(2)
According to github.com/apple/swift-evolution/blob/main/proposals/… the method in Obj-C should be called updateStatusCompletionHandler: or something similar.Histone
That works! The Xcode autocomplete doesn't list it in the dropdown, but it does work! If you submit it as an answer I can mark it as such.Seafaring
H
20

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.

Histone answered 23/11, 2021 at 19:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.