RobLabs
RobLabs

Reputation: 2347

Create a single Swift Package that includes two or more `target`s

We have designed a Swift Package that includes MapKit Extensions and some custom funcs that perform some Geographic computations. The intended usage would be to use a single Swift Package imported into Xcode to use two classes: one custom and the other extensions to MKMapView.

Is it possible to create a single Swift Package that includes two or more targets? How to properly update Package.swift to support multiple targets?

import MapKitSwiftExtensions
import CustomGeospatialComputations

The impression is that a second target in Package.swift can be added in the targets array.

.library(
  name: "MapKit Swift Extensions", 
  targets: ["MapKitSwiftExtensions", "CustomGeospatialComputations"]
)

But when CustomGeospatialComputations is added to the targets array, I get this error šŸ”“:

target 'CustomGeospatialComputations' referenced in product 'MapKit Swift Extensions' could not be found


Ideally, I would like to have this file structure for development.

.
ā”œā”€ā”€ Package.swift
ā”œā”€ā”€ README.md
ā”œā”€ā”€ Sources
ā”‚Ā Ā  ā””ā”€ā”€ CustomGeospatialComputations
ā”‚Ā Ā      ā””ā”€ā”€ GeoComputations.swift
ā”‚Ā Ā  ā””ā”€ā”€ MapKitSwiftExtensions
ā”‚Ā Ā      ā””ā”€ā”€ MKMapView+Extensions.swift

This is the Package.swift that works (without CustomGeospatialComputations)

// swift-tools-version:5.2

import PackageDescription

let package = Package(
    name: "MapKit Swift Extensions",
    products: [
        .library(
            name: "MapKit Swift Extensions"],
            targets: ["MapboxSwiftExtensions"),
    ],
    dependencies: [ ],
    targets: [
        .target(
            name: "MapKitSwiftExtensions",
            dependencies: []),
    ]
)

Upvotes: 5

Views: 3099

Answers (1)

Caleb Kleveter
Caleb Kleveter

Reputation: 11494

Based on the error message, I'm pretty sure you're forgetting to define the CustomGeospatialComputations target in your manifest's targets array. Your complete manifest should look like this:

// swift-tools-version:5.2

import PackageDescription

let package = Package(
    name: "MapKit Swift Extensions",
    products: [
        .library(
            name: "MapKit Swift Extensions",

            // Add the targets to your product.
            targets: ["MapKitSwiftExtensions", "CustomGeospatialComputations"]),
    ],
    dependencies: [ ],
    targets: [
        .target(
            name: "MapKitSwiftExtensions",
            dependencies: []),

        // Define the target for the package.
        .target(
            name: "CustomGeospatialComputations",
            dependencies: []),
    ]
)

Upvotes: 5

Related Questions