2

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?

Rivera
  • 10,792
  • 3
  • 58
  • 102

1 Answers1

1

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
Asperi
  • 228,894
  • 20
  • 464
  • 690