Xcode 11, importing types from Swift packages into Obj-C


Is it possible to use types defined in Swift packages in Obj-C code in Xcode 11?

This document: https://developer.apple.com/documentation/swift/imported_c_and_objective-c_apis/importing_swift_into_objective-c (assuming that I understand it correctly) seems to imply that Xcode will auto-generate appropriate headers to allow the user to reference Swift types in Obj-C code.

I would like to use a Swift package in an Obj-C codebase that I have for an old macOS Cocoa application. In Xcode 11 it appears to be possible to add Swift packages to projects, even if they are Obj-C based.

The documentation linked above states:

You can work with types declared in Swift from within the Objective-C code in your project by importing an Xcode-generated header file. This file is an Objective-C header that declares the Swift interfaces in your target, and you can think of it as an umbrella header for your Swift code. You don’t need to do anything special to create the generated header—just import it to use its contents in your Objective-C code.

The header's name is generated from your product module name, followed by "-Swift.h" .


When you're building an app target, you can import your Swift code into any Objective-C .m file within that same target using this syntax and substituting the appropriate name:

#import "ProductModuleName-Swift.h"

However, when I try to test this by using the sample Swift Library 'DeckOfCards' Xcode cannot find the generated header.

Am I being an idiot, or is this something that isn't intended to be supported in the Xcode / Obj-C / Swift / SPM interoperability?


(from DeckOfPlayingCards/Package.swift)

let package = Package(
     name: "DeckOfPlayingCards",
     products: [
         .library(name: "DeckOfPlayingCards", targets: ["DeckOfPlayingCards"]),

Some more searching turned-up this thread

It looks as if Swift 5.2 is required (and whatever version of Xcode supports it.)

1 Like

I meet the same problem, basically we have internal swift modules need to be imported into a ObjC based app workspace, and after we imported the <SwiftModule/SwiftModule-Swift.h> in objc .m files, we found that the XCode reports all the interfaces from the SwiftModule-Swift.h as undefined.

But the wired thing is that: in the <SwiftModule/SwiftModule-Swift.h> file, all the interfaces are already auto generated...

Terms of Service

Privacy Policy

Cookie Policy