How to Fix iOS Swift-Objective-C Bridging Header Error
In this tutorial, you'll learn about How to Fix iOS Swift. We cover key concepts, practical examples, and best practices.
The Problem
You add Objective-C code to a Swift project and get:
'MyObjCClass.h' file not found
Or:
Failed to import bridging header
'/path/to/Project/Project-Bridging-Header.h'
Xcode cannot find or parse the bridging header file that connects Objective-C to Swift.
Quick Fix
Step 1: Create the bridging header
Xcode prompts to create a bridging header when you add the first .m file to a Swift project. If you declined, create it manually:
File > New > File > Header File > Name it YourProject-Bridging-Header.h.
Step 2: Set the bridging header path in build settings
Build Settings > Objective-C Bridging Header:
$(SRCROOT)/YourProject/YourProject-Bridging-Header.h
The path is relative to $(SRCROOT) (the project folder). Use $(SRCROOT)/Target/Bridging-Header.h if the file is in a subfolder.
Step 3: Import Objective-C headers in the bridging header
// YourProject-Bridging-Header.h
#import "MyObjCClass.h"
#import "SomeOldLibrary.h"
Do not import UIKit or Foundation in the bridging header -- those must be imported in the individual .m files.
Step 4: Verify target membership
Select the bridging header file in the Project Navigator. In the File Inspector, verify it is a member of your app target. Without target membership, the compiler cannot find it.
Step 5: Check the Objective-C file's target membership
Every .m file that the bridging header references must also be a member of the same target.
Step 6: Use @class forward declarations
// Instead of importing everything, forward-declare where possible
@class MyObjCClass;
// Then in the bridging header, import only what Swift needs to see
#import "MyObjCClass.h"
Step 7: Import Swift code in Objective-C
Xcode generates a -Swift.h header automatically:
// In an Objective-C .m file
#import "YourProject-Swift.h"
This header exposes Swift classes marked with @objc to Objective-C.
Step 8: Mark Swift classes as @objc
@objc class MySwiftClass: NSObject {
@objc func doSomething() {
print("Hello from Swift")
}
}
Only @objc members are visible in the generated -Swift.h header.
Prevention
- Create the bridging header when Xcode first prompts you.
- Keep the bridging header clean -- import only what Swift needs.
- Use
@objcand@objcMembersdeliberately for Swift-to-ObjC exposure.
Common Mistakes with swigift bridge
- Forgetting
deriving (Show, Eq)on custom data types needed for debugging - Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
- Using
headandtailinstead of pattern matching, causing runtime errors on empty lists
These mistakes appear frequently in real-world IOS code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
DodaTech Tool Reference
Doda Browser's Source Inspector can verify bridging header syntax and path correctness before building, reducing trial-and-error during setup.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro