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

Viewed 1124

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: []),
    ]
)

1 Answers

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: []),
    ]
)
Related