Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Consume Swift Package for multiple targets and platforms in a project?

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:

SPM1

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:

SPM2

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]
)
like image 859
TruMan1 Avatar asked Aug 23 '19 12:08

TruMan1


People also ask

How do I add a Swift package to an existing project?

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.

What is the difference between project and target in Xcode?

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.

Can I use Swift package in Objective-C?

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.


2 Answers

I had the same problem, and I only found this two solutions:

First, add the package to the first target:

Add package

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:

Adding package in General tab

The other option is going to build Phases and repeat a similar way in Link Binary With Libraries:

Build Phases

Select package

At the moment, I only know this options, I hope in the future Apple could improve this with a multi-check, for example.

like image 116
Diego Carrera Avatar answered Oct 19 '22 03:10

Diego Carrera


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

like image 27
Sanzaru Avatar answered Oct 19 '22 03:10

Sanzaru