I have a project with multiple targets, such as an iOS app, a watchOS app, and some frameworks. How can I assign the same Swift Package to all my targets? Xcode only let's me select one:
If I try to add the Swift Package again so I can try assigning it to another target in my project, I get an error:
What is the correct way to do this? Below is what the package manifest looks like in the Swift Package. Is there something to be done on that side or something I have to do different in Xcode?
import PackageDescription
let package = Package(
name: "Alamofire",
platforms: [
.macOS(.v10_12),
.iOS(.v10),
.tvOS(.v10),
.watchOS(.v3)
],
products: [
.library(
name: "Alamofire",
targets: ["Alamofire"])
],
targets: [
.target(
name: "Alamofire",
path: "Source")
],
swiftLanguageVersions: [.v5]
)
To add a new Swift package dependency: Select your project in the Project navigator, then select your app target and navigate to its General pane. Click the + button in the "Frameworks, Libraries, and Embedded Content" section, select the local package's library product, and add it as a dependency.
A target defines a single product; it organizes the inputs into the build system—the source files and instructions for processing those source files—required to build that product. Projects can contain one or more targets, each of which produces one product.
As of today 10/10/19, you cannot have mixed Swift and Objective-C files in the same package's source code. (They may exist in the same 'Source' directory since you can explicitly exclude files from being compiled) You should not need to change your projects directory structure to build your package.
I had the same problem, and I only found this two solutions:
First, add the package to the first target:
Then, the first option is going to the other target, General tab, and in Frameworks, Libraries and Embedded Content press +, select the package and press Add:
The other option is going to build Phases and repeat a similar way in Link Binary With Libraries:
At the moment, I only know this options, I hope in the future Apple could improve this with a multi-check, for example.
In addition to the solution diego-carrera gave I had to reset the swift package caches to have the package available for all targets in the framework dialog.
In Xcode: File -> Swift Packages -> Reset Package Caches
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With