Skip to content

Mixing Swift and Objective-C: bridging headers and the generated Swift header

Plenty of Apple-platform codebases still have Objective-C in them, and new code is usually written in Swift. Xcode lets both languages live in the same target, and the two directions work differently:

  • Objective-C → Swift: you write a bridging header that lists the Objective-C headers Swift should see.
  • Swift → Objective-C: the compiler generates a header named <ProductModuleName>-Swift.h. You don’t write it; you import it.

The examples below assume an app target. Frameworks work slightly differently, covered at the end.

When you add the first Objective-C file to a Swift app target (or the first Swift file to an Objective-C one), Xcode offers to create a bridging header for you. To do it by hand:

  1. Add a header file named <ProjectName>-Bridging-Header.h. The name is only a convention.

  2. Import the Objective-C headers you want Swift to see:

    #import "MyObjectiveCClass.h"
  3. In the target’s build settings, set Objective-C Bridging Header (SWIFT_OBJC_BRIDGING_HEADER) to its path, for example $(SRCROOT)/MyApp/MyApp-Bridging-Header.h.

Everything imported there is visible to every Swift file in the target, with no import statement needed.

Given this Objective-C class:

MyObjectiveCClass.h
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface MyObjectiveCClass : NSObject
- (NSString *)greetingWithName:(NSString *)name;
- (void)printGreeting:(NSString *)greeting;
@end
NS_ASSUME_NONNULL_END

Swift sees it with Swift-style names. greetingWithName: becomes greeting(withName:):

let objCObject = MyObjectiveCClass()
let greeting = objCObject.greeting(withName: "John")
print(greeting) // a String, not String?
objCObject.printGreeting("Hello") // String is bridged to NSString

The NS_ASSUME_NONNULL_BEGIN/END block matters. Without nullability annotations, every object pointer is imported as an implicitly unwrapped optional (String!), and let greeting = ... would infer String?. Annotate headers with nonnull/nullable (or the audited region above) so Swift gets accurate types. If the automatic name is awkward, NS_SWIFT_NAME lets you pick the Swift name from the Objective-C side.

Only declarations Objective-C can represent make it into the generated header:

  • Classes must inherit from NSObject (or another Objective-C class). Swift structs, generic types and most enums can’t be exposed; an enum can be if it’s @objc with an integer raw type.
  • Methods and properties need @objc, or put @objcMembers on the class. Since Swift 4, subclassing NSObject no longer exposes members automatically.
  • In an app target, public and open declarations are included, and internal ones are too when the target has an Objective-C bridging header.
import Foundation
class MySwiftClass: NSObject {
@objc func greeting(withName name: String) -> String {
"Hello, \(name)!"
}
}

In any .m file in the same target:

#import "MyObjectiveCFile.h"
#import "MyApp-Swift.h" // <ProductModuleName>-Swift.h
@implementation MyObjectiveCFile
- (void)useSwiftClass {
MySwiftClass *swiftObject = [[MySwiftClass alloc] init];
NSString *greeting = [swiftObject greetingWithName:@"John"];
NSLog(@"%@", greeting);
}
@end

Characters in the product module name that aren’t valid in an identifier are replaced with underscores, so check the Product Module Name build setting if the import can’t be found. Import the generated header only in .m files; in a .h file, forward-declare the Swift class with @class MySwiftClass; to avoid circular imports.

Common Foundation types convert automatically when they cross the boundary:

Swift typeObjective-C type
StringNSString
ArrayNSArray
DictionaryNSDictionary
SetNSSet
DataNSData
DateNSDate
URLNSURL
Int, Double, BoolNSNumber (when passed as an object, e.g. in a collection)

Collections coming from Objective-C are untyped ([Any]) unless the header uses lightweight generics, such as NSArray<NSString *> *, which Swift imports as [String].

A framework target can’t have a bridging header. Objective-C headers that Swift should see go in the framework’s umbrella header (and must be public), and only public/open Swift declarations appear in the generated header. Import it with the module path:

#import <MyFramework/MyFramework-Swift.h>
  • Expose to Objective-C only what it needs. Every @objc member adds Objective-C metadata and thunks to the binary, which is why Swift 4 stopped inferring @objc (SE-0160).
  • Migrate incrementally: new features in Swift, old code converted a class at a time, starting with leaf classes that nothing else in Objective-C depends on.
  • Add nullability and lightweight generics to Objective-C headers before calling them from Swift. It’s cheap and removes most of the optionals and casts.

© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js · Logos:Font Awesome Free (CC BY 4.0)