Mark the Swift class as @objc (and make it inherit from NSObject), then import the generated header #import "YourTarget-Swift.h" in the .m file that uses it.
import Foundation
@objcMembers
public class Greeter: NSObject {
public func greet(name: String) -> String {
"Hello, \(name)"
}
}// ViewController.m
#import "MyApp-Swift.h" // target name, spaces and dashes become underscores
- (void)viewDidLoad {
[super viewDidLoad];
Greeter *greeter = [[Greeter alloc] init];
NSLog(@"%@", [greeter greetWithName:@"Ada"]);
}- Import the Swift header in
.mfiles, not in other headers. In a.hfile use a forward declaration:@class Greeter;. - In a framework target, use
#import <MyFramework/MyFramework-Swift.h>and make sure Defines Module is Yes. - Only features that exist in Objective-C are exposed: structs, generics, tuples and enums without raw
Intvalues are not visible. - If the header is missing, build the Swift file once so Xcode generates it, and check the product module name in Build Settings.
