Swift 包管理器(SPM)
[iOS 依赖管理器]
使用: [ SPM管理依赖项]
生产:如果您正在开发库/模块(模块化),您应该注意支持它
Package.swift
- 清单文件。
- 这是一个硬编码的名称。
- 应放在同一级别或更高级别,在其他情况下:
target '<target_name>' in package '<package_name>' is outside the package root
Package.swift 示例
// swift-tools-version:5.3
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "PackageName1",
//Supported platforms
platforms: [
.iOS(.v11)
],
//Product list - executable binary
products: [
.library(
name: "LibraryName1",
targets: ["TargetName1"]),
],
//Additional SPM dependencies declaration(packages) which are used in targets
dependencies: [
//Local package path
.package(name: "PackageName2", path: "../Feature1")
//Local package URL
.package(name: "PackageName2", url: "file:///some_local_path", .branch("master"))
//Remote package URL
.package(name: "PackageName2", url: "https://github.com/user/repo", .branch("master")),
],
targets: [
//Source code location. implicitly `Sources/<target_name>`(Sources/TargetName1) or explicitly `path:`
.target(
name: "TargetName1",
dependencies: [
//using dependencies from package.targets and package.dependencies
//package.targets
"LibraryName2"
//package.dependencies
.product(name: "PM2LibraryName1", package: "PackageName2")
]),
path: "Sources/TargetName1"
]
)
对 Swift 依赖的观察
import Module1
import Module2
- Swift 库公开
.modulemap umbrella.h
[About]用于公开 Objective-C 消费者思想的模块@import SomeModule;
- You are able to work with `Package.swift` in Xcode if double click on it
.swiftpm/xcode with .xcworkspace will be generated
- When you work with `Package.swift` you are able to check it, just save this file
- When you work with `Package.swift` you should specify schema and device(platform)
- When you build `Package.swift`
- library and .swiftmodule is located:
<path_to_derived_data>/DerivedData/<folder_name_where_Package.swift_located>-<randomizer>/Build/Products/<platform>
//e.g.
/Users/alex/Library/Developer/Xcode/DerivedData/someFolder-ccivqbbjujxuvdcxtuydyqfeepfx/Build/Products/Debug-iphonesimulator
- .modulemap and umbrella.header is located:
<path_to_derived_data>/DerivedData/<folder_name_where_Package.swift_located>-<randomizer>/Build/Intermediates.noindex/<project_name>.build/<platform>/<target_name>.build/<.modulemap> and plus /Objects-normal/<arch>/<tarhet_name-Swift.h>
- When you build consumer with `Package.swift` results files will be located:
<path_to_derived_data>/DerivedData/<target_name>-<randomizer>/Build/Products/<platform>
- When you work with consumer of `Package.swift` SPM clones it into
<path_to_derived_data>/DerivedData/<target_name>-<randomizer>/SourcePackages/checkouts
package.products.library.targets
您可以指定多个目标。这意味着可以使用来自单个可执行二进制文件的多个模块(一种伞库)
producer:
package.products.library.targets: ["TargetName1", "TargetName2"]
consumer:
1. adds single library
2. several imports
import TargetName1
import TargetName2
package.targets.target.dependencies
如果您的目标具有依赖性。您将获得相同的效果 - 伞库。
1.从同一个包中添加另一个目标
例如,使用Explicit Dependency
[关于]。重要提示:使用相同的名称(模块和 SPM 目标)。如果你没有设置依赖,package.targets.target.dependencies
你会得到下一个错误
Undefined symbol
2.从另一个包中添加产品。其他包中的所有目标都将被暴露
2.1 本地包路径
您不能在消费者方面使用。但它至少可以让你首次亮相
package <package_name_1> is required using a revision-based requirement and it depends on local package <package_name_2>, which is not supported
2.2 本地包地址
不要
在路径中使用空格(),否则你会得到
The URL file:///hello world/some_local_path is invalid
如果你不指定// swift-tools-version:<version>
,你会得到:
package at '<some_path>' is using Swift tools version 3.1.0 which is no longer supported; consider using '// swift-tools-version:5.3' to specify the current tools version
*不要忘记在测试之前提交您的更改并更新[关于]