Fix: Generated Swift Header Missing in Xcode Objective-C Project

Fix: Generated Swift Header Missing in Xcode Objective-C Project

Written by

in

Quick answer: The generated Swift header (*-Swift.h) is missing because the Product Module Name contains spaces or Defines Module is disabled. Fix by removing spaces from Product Module Name, enabling Defines Module in Build Settings > Packaging, and importing the header only in .m files, not .h files.

Why this happens

When you add Swift files to an existing Objective-C project, Xcode should automatically generate a header named ProductModuleName-Swift.h that exposes @objc classes and NSObject subclasses to Objective-C. This header fails to appear for three common reasons:

  1. Product Module Name contains spaces — The module name becomes part of the generated header filename. Spaces break the include path and prevent the header from being created.
  2. Defines Module is set to No — This build setting controls whether Xcode produces a module map and the Swift header. When disabled, the header is never emitted.
  3. Importing in a .h file — The generated header is not an umbrella header; it can only be imported in implementation (.m) files. Including it in a header causes a “file not found” error even when the header exists.

These issues date back to Xcode 6 but still occur in modern Xcode when migrating legacy codebases or creating new mixed-language targets.

How to fix it

1. Remove spaces from Product Module Name

Open the target’s Build Settings, search for Product Module Name (PRODUCT_MODULE_NAME). Replace any value containing spaces with a single PascalCase or camelCase identifier (e.g., MyApp instead of My App).

# Before
PRODUCT_MODULE_NAME = "My App"

# After
PRODUCT_MODULE_NAME = "MyApp"

After changing this, clean the build folder (Product → Clean Build Folder) and rebuild. The header will now be generated as MyApp-Swift.h.

2. Enable Defines Module

In the same Build Settings tab, search for Defines Module (DEFINES_MODULE) under the Packaging section. Set it to Yes.

DEFINES_MODULE = YES

This tells Xcode to create a module map and emit the Swift interface header. Without it, the compiler treats the target as a non-modular binary and skips header generation.

3. Import the header only in .m files

In your Objective-C implementation files, import the generated header using the module name you set:

// MyViewController.m
#import "MyApp-Swift.h"

@implementation MyViewController
- (void)useSwiftClass {
    MySwiftClass *obj = [[MySwiftClass alloc] init];
    [obj doSomething];
}
@end

Do not place this import in any .h file. Doing so breaks the module dependency chain and produces a “file not found” error. If you need to reference a Swift class in a header, forward-declare it with @class MySwiftClass; and keep the import in the .m.

4. Verify Swift classes are exposed

Only classes that are @objc or inherit from NSObject (or another Objective-C class) appear in the generated header. Pure Swift structs, enums, and classes without @objc are not visible. Ensure your Swift types meet one of these criteria:

// Visible to Objective-C
@objc class MySwiftClass: NSObject { }

// Also visible (NSObject subclass)
class MyOtherClass: NSObject { }

// NOT visible
struct NotVisible { }
class AlsoNotVisible { }

How to prevent it

  • Use a single-word, space-free Product Module Name from project creation.
  • Keep Defines Module = Yes for any target that mixes Swift and Objective-C.
  • Adopt the convention: @import ModuleName; in Swift, #import "ModuleName-Swift.h" in Objective-C .m files only.
  • Add a Build Phase → Run Script that validates the header exists after build (optional CI guard).
  • Document the module name in your project’s onboarding guide so new targets stay consistent.

FAQ

Why does the header appear in DerivedData but not in the project navigator?

The generated header is a build artifact placed in DerivedData/.../Build/Products/<config>/<Target>.framework/Headers/ (or the equivalent module cache). It is not a source file, so Xcode does not list it in the navigator. Import it by name; the compiler finds it via the module map.

Can I rename the generated header?

No. The filename is always $(PRODUCT_MODULE_NAME)-Swift.h. Changing the Product Module Name changes the header name. Keep the module name stable to avoid updating imports across the codebase.

What if I have multiple targets sharing Swift code?

Each target that contains Swift and needs Objective-C visibility must have its own Product Module Name and Defines Module = Yes. The generated header is per-target. Share Swift source files via a framework target or Swift package, then import the framework’s module in both Swift and Objective-C.

Sources & credits

Parts of this guide are adapted from content licensed under CC BY-SA 4.0; this article is shared under the same license.