pschwamb
pschwamb

Reputation: 809

Is there a way to include headers in Swift Package

I am trying to use a legacy framework as a Swift package. This framework has an umbrella header which imports UIKit via #import <UIKit/UIKit.h>, so the swift source files do not explicitly import UIKit. Building via SPM fails with error messages like: error: cannot find type 'UIFont' in scope. If I change the source files to explicitly import UIKit, these errors go away, but it seems there should be a Swift Package Manager equivalent to allowing a wider scope import.

See this PR for the specific project and my current workaround: https://github.com/i-schuetz/SwiftCharts/pull/419

Upvotes: 2

Views: 1884

Answers (1)

Chip Jarred
Chip Jarred

Reputation: 2805

The Swift package needs to have a Swift source file that re-exports UIKit

@_exported import UIKit

You only need one of these per target in your package that you want to act as an umbrella.

If the legacy framework isn't yours, you can create your own Swift package whose library target declares a dependency on the framework and which re-exports the framework and UIKit, even if all it has in it is a single file with the @_exported import statements. Then to use it in your project, you'd import your wrapper package instead of the original framework package.

I think you can also do it with a module map, but I'd need to do some research on exactly how.

Upvotes: 4

Related Questions