While modularizing a codebase, you may encounter Objective-C code that you'd like to move out of your app's target and into a target within a Swift package that your app target depends on.
With Swift dependencies, this is straightforward:
- Create a folder inside the
Sourcesdirectory of the Swift package.
- Delete the references to the Swift files you'd like to move over from the main target.
- Drag and drop the files from their location in the main target to the folder you just created in
- Update the Package.swift to reference the new folder:
let package = Package( name: "...", platforms: [...], products: [ .library(name: "YourNewLibrary", targets: ["YourNewLibrary"]), ... ], dependencies: [...], targets: [ .target(name: "YourNewLibrary", dependencies: [...], path: "Sources/YourNewLibrary") ... ] )
With Objective-C dependencies, the steps are exactly the same as above. However, we have to help Swift Package Manager identify where the dependency's header files are located. To do this, you can either:
- Create a folder called
includewithin your target folder (e.g.
Sources/YourNewLibrary/include/) and move your Objective-C header files into that folder.
- Specify a custom path for the headers folder using
publicHeadersPath(this is set to
includeby default). For example:
.target(name: "YourNewLibrary", dependencies: [...], path: "Sources/YourNewLibrary", publicHeadersPath: "Core/headers/")
With either option, all Objective-C headers must be located within the same directory, and this directory cannot contain any implementation files.
Apple Developer Documentation -