#20: Calling Objective-C from Swift

On Apple platforms, Swift can call Objective-C code directly. The usual setup is to expose the Objective-C headers you need in a bridging header.

Add the header to your target and import the Objective-C type:

// ProjectName-Bridging-Header.h
#import "LegacyManager.h"

Then use it in Swift as if it were a native API:

let manager = LegacyManager()
manager.startSync()
manager.setValue("hello", forKey: "message")

Objective-C selectors are imported using Swift naming conventions, nullable annotations become optionals, and Foundation types are bridged automatically.

If you need to go in the opposite direction and expose Swift back to Objective-C, mark compatible APIs with @objc (or inherit from NSObject when appropriate).


I'm also on Twitter and GitHub.

Subscribe via RSS.