swift package ma
20 April 2023
Swift Package Manager 使用指南
参考: https://juejin.cn/post/6844903544017911821 https://github.com/apple/swift-package-manager/blob/main/Documentation/Usage.md https://docs.vapor.codes/zh/getting-started/spm/
几个常用的命令
swift package init --type executable(or library) //初始化包,不带参数的话默认是Library
swift package init //初始化包,不带参数的话默认是Library
swift build # 构建 lib
swift run # 运行 lib
使用过程
MyLib
mkdir MyLib && cd MyLib
swift package init //初始化包,不带参数的话默认是Library
swift build //编译并生成可执行的二进制文件
swift run //执行生成的文件
Packages.swift
import PackageDescription
let package = Package(
name: "MyLib",
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
name: "MyLib",
targets: ["MyLib"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.target(
name: "MyLib",
dependencies: []),
.testTarget(
name: "MyLibTests",
dependencies: ["MyLib"]),
]
)
Source MyLib.swift
public struct MyLib {
public private(set) var text = "Hello, World!"
public var num:Int
public init() {
num = 2
}
}
SPM依赖包必须使用git url和版本号,所以我们需要为我们的库创建一个git仓库并提交代码和打tag:
$ git init
$ git add .
$ git commit -m "Initial Commit"
$ git tag 1.0.0
生成 MyLib.xcodeproj:swift package generate-xcodeproj,以便xcode其他项目引入
MyPackage
mkdir MyPackage && cd MyPackage
swift package init //--type executable
swift build //编译并生成可执行的二进制文件
swift run //执行生成的文件
//生成MyPackage.xcodeproj
swift package generate-xcodeproj
swift build --configuration release //构建release lib
Packages.swift
import PackageDescription
let package = Package(
name: "MyPackage",
products:[
// Products define the executables and libraries produced by a package, and make them visible to other packages.
.library(
name: "MyPackage",
type: .static,
targets: ["MyPackage"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
.package(url: "https://github.com/SwiftyJSON/SwiftyJSON.git", from: "4.0.0"), //第三方库url和版本号
.package(url: "../MyLib", from: "1.0.0")
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.target(
name: "MyPackage",
dependencies: ["SwiftyJSON","MyLib"]),
.testTarget(
name: "MyPackageTests",
dependencies: ["MyPackage","MyLib"]),
]
)
Source MyPackage.swift
import SwiftyJSON //引入模块
import MyLib
public struct MyPackageLib {
public private(set) var text = "Hello, MyPackageLib!"
public var num:Int
public init() {
num = 3
print(text)
let json = JSON(["name":"Jack", "age": 25])
print(json)
//自定义库
let lib = MyLib()
print(lib)
}
}
在iOS工程中使用SPM
- 使用xcode创建一个Example.xcworkspace
- 使用xcode创建一个Example.xcodeproj,并将
Example.xcodeproj与MyPackage.xcodeproj拖到 Example workspace当中 - 配置xcode项目,在Example的Target中手动添加MyPackage.framework
