How to call Swift from Objective-C

Gerriet M. Denkmann

I have a Swift project called TestSwift:

import Cocoa

class AppDelegate: NSObject, NSApplicationDelegate
@IBOutlet weak var window: NSWindow!

func applicationDidFinishLaunching(_ aNotification: Notification)
print(#function + " self \(self)") // self <TestSwift.AppDelegate: 0x600000000820>
let a = OClass()

public func abc() { print(#function) }

I read somewhere that Xcode would generate a TestSwift-Swift.h header for me.
My Xcode (Version 9.4.1 (9F2000)) did not.

So I made:

@interface AppDelegate : NSObject

Here is my Objective-C class:

#import "OClass.h"
#import "TestSwift-Swift.h"
@implementation OClass
- (void)soSomethingFor: (AppDelegate *)sender;
NSLog(@"%s sender %@",__FUNCTION__, sender); // sender <TestSwift.AppDelegate: 0x600000000820>
[ sender abc ]; // -[TestSwift.AppDelegate abc]: unrecognized selector sent to instance 0x600000000820

Compiles fine, but gets this “unrecognized selector” error at run time.

What am I doing wrong?


Join { to automatically receive all group messages.