Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
228 views
in Technique[技术] by (71.8m points)

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

question from:https://stackoverflow.com/questions/65887860/create-a-single-swift-package-that-includes-two-or-more-targets

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...