Swift Package Manager(SPM)是苹果最新的官方工具,可以用来管理 Swift 代码的分配,它为 Swift 编译系统集成了自动进行下载、编译和连接依赖的过程。
在此之前我们常用的工具是Cocoapods和Carthage。
支持的平台
✔ macOS
✔ Linux
✘ iOS
✘ watchOS
✘ tvOS
测试环境
系统版本
10.11.5 Beta
Swift版本
Xcode7.3.1(swift 2.2)
Xcode8 beta(swift 3)
检查当前Swift版本是否支持SPM
当前默认为Xcode7.3.1,查看swift版本
swift -version
Apple Swift version 2.2 (swiftlang-703.0.18.8 clang-703.0.31)
Target: x86_64-apple-macosx10.9
swift2.2不支持SPM
swift build
error: unable to invoke subcommand: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-build (No such file or directory)
切换到Xcode8
sudo xcode-select -s /Applications/Xcode-beta.app
查看当前swift版本
swift -version
Apple Swift version 3.0 (swiftlang-800.0.30 clang-800.0.24)
Target: x86_64-apple-macosx10.9
Swift3支持SPM
swift build
error:: no Package.swift found
这个error是因为我们还没有创建SPM的库
创建SPM测试库
初始化
在github创建SPMTest测试库
https://github.com/huhuegg/SPMTest.git
clone SPMTest
cd /Users/admin/Swift/SwiftPackageManagerExample/
git clone https://github.com/huhuegg/SPMTest.git
初始化SPMTest library
cd /Users/admin/Swift/SwiftPackageManagerExample/SPMTest
swift package init --type library
Creating library package: SPMTest
Creating Package.swift
Creating .gitignore
Creating Sources/
Creating Sources/SPMTest.swift
Creating Tests/
Creating Tests/LinuxMain.swift
Creating Tests/SPMTest/
Creating Tests/SPMTest/SPMTestTests.swift
修改SPMTest
编辑Package.swift
import PackageDescription
let package = Package(
name: "SPMTest"
)
//创建动态链接库
//let dynamicLibSPMTest = Product(name: "SPMTest", type: .Library(.Dynamic), modules: "SPMTest")
//products.append(dynamicLibSPMTest)
//创建静态库
//let staticLibExecTest = Product(name: "SPMTest", type: .Library(.Static), modules: "SPMTest")
//products.append(staticLibExecTest)
编辑SPMTest.swift
必须包含init方法,否则使用时会遇到 initializer is inaccessible due to 'internal' protection level
public class SPMPublicTest {
public var text = "SPMPublicTest"
public init(text:String) {
self.text = text
}
}
提交并标注SPMTest版本
提交编辑后的SPMTest
git commit * -m "init library, add tag 1.0.0"
git push
添加版本号Tag
此处需要注意: commit && push的时候local tag默认不会被提交
git tag 1.0.0
git push origin --tags
创建测试工程
mkdir /Users/admin/Swift/SwiftPackageManagerExample/ExecExampleTest
cd /Users/admin/Swift/SwiftPackageManagerExample/ExecExampleTest
swift package init --type executable
Creating executable package: SwiftPackageManagerExample
Creating Package.swift
Creating .gitignore
Creating Sources/
Creating Sources/main.swift
Creating Tests/
将SPMTest库导入工程
在Package.swift的dependencies添加SPMTest,并指定版本为1.0.0
import PackageDescription
let package = Package(
name: "ExecExampleTest",
dependencies: [
.Package(url:"https://github.com/huhuegg/SPMTest.git", majorVersion: 1),
]
)
添加测试代码
不要在main.swift直接导入SPMTest
创建Test.swift
vi Sources/Test.swift
import SPMTest
class Test {
class func testFunc() {
let spm = SPMPublicTest(text: "SPMTest::SPMPublicTest")
print("testFunc: \(spm.text)")
}
}
修改main.swift
print("## Swift Package Manager Example ##")
Test.testFunc()
编译
创建测试工程的时候使用的类型是executable,所以最后会生成可执行文件
swift build
Cloning https://github.com/huhuegg/SPMTest.git
HEAD is now at 629d483 public
Resolved version: 1.0.0
Compile Swift Module 'SPMTest' (1 sources)
Compile Swift Module 'ExecExampleTest' (2 sources)
Linking .build/debug/ExecExampleTest
如果SPMTest库的tag没有被提交,在build时会遇到错误
error: No version tag found in (/Users/admin/Swift/example/Packages/SPMTest) package. Add a version tag with "git tag" command. Example: "git tag 0.1.0"
测试
.build/debug/ExecExampleTest
Swift Package Manager Example
testFunc: SPMTest::SPMPublicTest
回到Xcode
之前所有的操作都没有打开Xcode编辑器,让我们回到最熟悉的Xcode吧!
创建Xcode工程
swift package generate-xcodeproj
generated: ./ExecExampleTest.xcodeproj
芝麻开门
open ./ExecExampleTest.xcodeproj
调试运行时记得选My Mac, 强烈期待iOS iOS iOS~