-Swift.h file: Xcode generates this interface header when it builds the target. Import the header using the target’s Product Module Name, then make sure your Swift declarations are visible to Objective-C and the target builds successfully.Why this happens
An Objective-C file cannot import a Swift source file directly. Xcode generates an Objective-C interface header for compatible Swift declarations, normally named after the target’s Product Module Name followed by -Swift.h. For example, a module named MyProject normally produces MyProject-Swift.h.
The generated header is not the same as an Objective-C bridging header. The bridging header exposes Objective-C declarations to Swift; the generated interface header exposes Swift declarations to Objective-C. Creating an empty MyProject-Swift.h yourself does not give Objective-C access to your Swift code.
An error such as 'myProjectModule-Swift.h' file not found can mean the import uses the wrong module name, the Swift file is not included in the target, or Swift compilation has failed. A separate problem is that the header exists but a class or method is not representable in Objective-C.
How to fix it
1. Remove the manual header and use the generated name
- If you created a
-Swift.hfile by hand, remove that file from the project. Do not remove your bridging header. - Select your app target in Xcode and open Build Settings.
- Search for Product Module Name. Note its resolved value for the build configuration you are using.
- Search for Objective-C Generated Interface Header Name and check the header name shown there. By default, it uses the module name followed by
-Swift.h. - Import that name in the Objective-C implementation file that needs the Swift class.
For a module named MyProject, the import normally looks like this:
// MainMenu.m
#import "MainMenu.h"
#import "MyProject-Swift.h"
Replace MyProject with your target’s actual module name; do not copy the placeholder from this example. The module name is not necessarily identical to the project’s display name. If the generated interface header name has been customized, use the name in the build setting instead.
Build the target after adding the import. The generated header is a build product, so you should not expect to maintain it as a normal source file in the project navigator.
2. Make the Swift class and methods visible to Objective-C
A header import alone does not expose every Swift declaration. The class and the members you call must be compatible with Objective-C and exposed to it. @objcMembers is a convenient option for a small NSObject subclass with Objective-C-compatible members.
Add a Swift file to the app target:
import Foundation
@objcMembers
class FBManager: NSObject {
func message() -> String {
return "Hello from Swift"
}
}
With the generated header imported, an Objective-C implementation in the same app target can use the class:
#import "MainMenu.h"
#import "MyProject-Swift.h"
@implementation MainMenu
- (void)showManagerMessage {
FBManager *manager = [[FBManager alloc] init];
NSLog(@"%@", [manager message]);
}
@end
This example uses the app target’s Objective-C bridging header for internal Swift declarations. If your app does not have one, create a header for the Objective-C declarations Swift needs and set its path in Objective-C Bridging Header. The generated interface header and the bridging header have different jobs; do not point both settings at the same file.
If only selected members should be visible, use @objc on the class and on the members Objective-C needs instead of applying @objcMembers to the whole class. Types in exposed method signatures must also be representable in Objective-C. For example, a method returning an Objective-C class can be exposed when that type is available to Swift and compatible with the generated interface.
3. Check target membership and build settings
If the import still fails, verify that Xcode is compiling the Swift file as part of the same target as the Objective-C implementation:
- Select the Swift file in the project navigator.
- In the file inspector, check Target Membership and select the intended target if necessary.
- In that target’s Build Settings, search for Install Objective-C Compatibility Header and verify that it is enabled.
- Search for Defines Module and enable it if your target needs a module, as in the original integration approach.
- Confirm the Product Module Name and generated interface header name again, especially if the project has multiple targets or configurations.
For a framework, Swift declarations intended for Objective-C clients generally need public or open access as well as Objective-C compatibility. An internal app-target example should not be assumed to form a framework’s public interface. Also check the framework target’s own settings rather than the app target’s settings.
If Swift needs to call an Objective-C class, add that class’s header to the bridging header for an app target. If Objective-C needs to call Swift, import the generated Swift interface in the Objective-C implementation. Seeing Objective-C types from Swift confirms only the first direction; it does not confirm that the reverse import is set up correctly.
4. Fix compiler errors, then rebuild
Swift compilation errors can prevent Xcode from producing a usable generated interface. Look at the build errors above the missing-header error and resolve errors in the Swift target first. Pay particular attention to invalid @objc declarations and method signatures that use types Objective-C cannot represent.
Build again after correcting the errors. If the settings and code are correct but Xcode still reports the old failure, clean the build folder and rebuild the target. Cleaning cannot fix a wrong module name or an unexposed Swift member, so check those first.
5. Keep the generated import out of Objective-C headers
Put the -Swift.h import in an .m file where possible. If an Objective-C header needs to refer to a Swift class by pointer, forward-declare it there and import the generated header in the implementation:
// MainMenu.h
#import <Foundation/Foundation.h>
@class FBManager;
@interface MainMenu : NSObject
@property (nonatomic, strong) FBManager *manager;
@end
// MainMenu.m
#import "MainMenu.h"
#import "MyProject-Swift.h"
@implementation MainMenu
@end
This avoids pulling the generated interface into a header that may itself be included by the bridging header, which can create an import cycle. A forward declaration is enough for a pointer property, but the implementation needs the full declaration to call the Swift class’s methods.
How to prevent it
- Check the target’s Product Module Name before writing the import.
- Let Xcode generate the Swift interface header; never maintain a hand-written replacement.
- Keep Objective-C-to-Swift bridging and Swift-to-Objective-C imports separate.
- Expose only Objective-C-compatible Swift members that the Objective-C code needs.
- Check target membership and build errors before cleaning derived build products.
FAQ
Why can Swift see my Objective-C classes, but Objective-C cannot see Swift?
The bridging header makes Objective-C declarations available to Swift. Objective-C uses the separately generated -Swift.h interface to see compatible Swift declarations, so one direction can work while the other is misconfigured.
Why is my Swift class missing even though the header imports?
The class or the member you need may not be exposed to Objective-C, may use an incompatible type, or may not have the required access level for the target. Check its annotations, signature, target membership, and—for framework clients—its access level.
Should I create the -Swift.h file myself?
No. Xcode generates the interface from eligible Swift declarations during the build. Delete a manually created file with that name and import the generated name from the target’s build settings instead.
Parts of this guide are adapted from content licensed under CC BY-SA 4.0; this article is shared under the same license.