iosobjective-cswiftbridging-headerobservableobject

Linker error when using Swift ObservableObject from Objective-C


I am trying to use a Swift class from Objective-c (as I do for many other classes) with the following error.

Undefined symbol: OBJC_CLASS$__TtC9FileCloud18BrowserCoordinator

The issue seems to be that this particular class conforms to the Swift protocol ObservableObject:

@available(iOS 13.0, *)
@objcMembers
class BrowserCoordinator: NSObject, ObservableObject
{
    //...
}

It properly shows up in the generated MyProject-Swift.h header file:

SWIFT_CLASS("_TtC9FileCloud18BrowserCoordinator") SWIFT_AVAILABILITY(ios,introduced=13.0)
@interface BrowserCoordinator : NSObject
//...
@end

And builds properly when used from Objective-C:

BrowserCoordinator* browser = BrowserCoordinator.new;

But then the linker fails. Could it be an Apple bug?


Solution

  • Here is tested & worked demo (with Xcode 12 / iOS 14)

    Swift:

    import SwiftUI
    import Combine
    
    @objcMembers
    public class BrowserCoordinator: NSObject, ObservableObject
    {
        public func go() {
            print(">> test")
        }
    }
    

    Objective-C:

    #import "MyViewController.h"
    #import "TestBacktoObjC-Swift.h"
    
    @interface MyViewController ()
    @end
    
    @implementation MyViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        BrowserCoordinator* browser = BrowserCoordinator.new;
        [browser go];
    }
    
    @end