简体   繁体   中英

Swift Package Manifest - What is the difference between library targets and target dependencies?

In Package.swift, what is the difference between adding a target the array of targets within a library versus adding a target to the array of dependencies.

In this first example, 'Helpers' is added as a dependency to the 'MySwiftPackage' target.

let package = Package(
    name: "MyTestPackage",
    products: [
        .library(
            name: "MyTestPackage",
            targets: ["MyTestPackage"]),
        .library(
            name: "Helper",
            targets: ["Helper"]),
    ],
    dependencies: [
    ],
    targets: [
        .target(
            name: "MyTestPackage",
            dependencies: ["Helper"]), // <---- 'Helper' defined as dependency
        .target(
            name: "Helper",
            dependencies: []),
    ]
)

In this next example, 'Helper' is added as another target within the 'MySwiftPackage' library.

let package = Package(
        name: "MyTestPackage",
        products: [
            .library(
                name: "MyTestPackage",
                targets: ["MyTestPackage","Helper",]), // <-- 'Helper' defined in targets
            .library(
                name: "Helper",
                targets: ["Helper"]),
        ],
        dependencies: [
        ],
        targets: [
            .target(
                name: "MyTestPackage",
                dependencies: []),
            .target(
                name: "Helper",
                dependencies: []),
        ]
    )

From what I can tell, both options produce the same result: when I declare MySwiftPackage as a dependency inside of an Xcode project, I am able to import both the 'MySwiftPackage' module and the 'Helper' module.

Is there a significant difference between the two manifest files other than where 'Helper' is placed?

From the Swift Package Manager Documentation

A target may build either a library or an executable as its product.
A library contains a module that can be imported by other Swift code. 
...

A library is something that can be imported by other swift code.

The dependencies that your library relies on are defined in the

.target(name: "MyTestPackage", dependencies: ["Helper"])

The targets could be thought of as the modules, organized in folders.

When you define the MyTestPackage , you declare its dependencies Helper in the targets array of the package. If you didn't declare it there, code in MyTestPackage won't be able to import Helpers.

In the first example, the library MyTestPackage 's target includes helpers as a dependency of MyTestPackage , so you could delete the Helper library and should still be able to import both MyTestPackage and Helper in your project.

In the second example, you declare both MyTestPackage and Helpers as targets that make up the MyTestPackage library, so again you should be able to delete the Helper library and still import code from both modules.

If you were to delete both libraries from either example, you would not be able to import any code into your project

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM