Swift Package Manager使用总结

7,523 阅读3分钟

一、简介

Swift Package Manager(以下简称SPM)是苹果在swift3.0中加入的一个包管理工具,用于处理模块代码的下载、编译和依赖关系等。跟CocoaPods和Carthage功能类似,不过比这两个更简洁,代码的侵入性更小,也不需要额外安装工具。

需要注意的是,SPM在swift4.0中重新设计了API,所以你需要确定你当前使用的swift版本,否则很可能会出现build失败的情况,以下代码都是基于swift4.0版本。

二、使用教程

既然是包管理,所以我们的操作都是以包的形式,依赖的第三方库都会当成一个个单独的包来处理,依赖包可以嵌套。

SPM包含两种包:可执行的二进制包(executable)和静态库包(Library),两者唯一的区别就是前者会生成二进制可执行文件,可以直接用命令行执行。如果创建的是Library,用命令行执行会提示没有可执行文件,这个时候只需要在Sources/目录下创建一个main.swift文件就可以执行了。

1、通过命令创建包:

$ mkdir SPMDemo    
$ cd SPMDemo         
$ swift package init --type executable(or library)  //初始化包,不带参数的话默认是Library
Creating executable package: SPMDemo
Creating Package.swift
Creating .gitignore
Creating Sources/
Creating Sources/main.swift
Creating Tests/
$ swift build    //编译并生成可执行的二进制文件
Compile Swift Module 'SPMDemo' (1 sources)
Linking ./.build/debug/SPMDemo
$ swift run 	//执行生成的文件
Hello, world!   //执行效果

2、加入依赖包

我们需要在前面创建的包中使用第三方库(这里以SwiftyJSON为例),编辑Package.swift文件:

// swift-tools-version:4.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "SPMDemo",
    dependencies: [
        // Dependencies declare other packages that this package depends on.
        .package(url: "https://github.com/SwiftyJSON/SwiftyJSON.git", from: "4.0.0"),	//第三方库url和版本号
    ],
    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 which this package depends on.
        .target(
            name: "SPMDemo",
            dependencies: ["SwiftyJSON"]),	//依赖的包名称
    ]
)

注:关于dependencies的格式定义可以看这里有详细说明文档,可以指定分支、具体的commit编号、版本范围等等。

然后我们检验一下引入的依赖包是否可用,编辑Sources目录下的main.swift文件:

import SwiftyJSON	//引入模块

let json = JSON(["name":"Jack", "age": 25])
print(json)

print("Hello, world!")

保存之后我们再run一下:

$ swift run
Fetching https://github.com/SwiftyJSON/SwiftyJSON.git
Cloning https://github.com/SwiftyJSON/SwiftyJSON.git
Resolving https://github.com/SwiftyJSON/SwiftyJSON.git at 4.0.0
Compile Swift Module 'SwiftyJSON' (1 sources)
Compile Swift Module 'SPMDemo' (1 sources)
Linking ./.build/x86_64-apple-macosx10.10/debug/SPMDemo
{
  "name" : "Jack",
  "age" : 25
}
Hello, world!

可以看到输出了我们的json结构体,说明引入依赖包成功了!

注:第一次run的时候SPM会先将依赖的库克隆下来并编译好放在.build隐藏文件夹中,如果把这个文件夹删除重新run,会重新下载。

3、现在我们来试试自己定义一个库作为其他项目的依赖包

$ mkdir MyLib    
$ cd MyLib         
$ swift package init	//初始化包,不带参数的话默认是Library
Creating library package: MyLib
Creating Package.swift
Creating README.md
Creating .gitignore
Creating Sources/
Creating Sources/MyLib/MyLib.swift
Creating Tests/
Creating Tests/LinuxMain.swift
Creating Tests/MyLibTests/
Creating Tests/MyLibTests/MyLibTests.swift

创建Library的话默认是没有main.swift文件的,Sources目录下只有一个MyLib.swift文件,它给我们定义了一个结构体,但并不是public的,我们知道swift中只有被public和open修饰才能被其他模块访问,所以我们把它改成public:

public struct MyLib {
    var text = "Hello, MyLib!"
    public var num:Int
    public init() {
        num = 2
    }
}

然后我们build一下,确保我们的Library能顺利编译通过。

因为SPM依赖包必须使用git url和版本号,所以我们需要为我们的库创建一个git仓库并提交代码和打tag:

$ git init
$ git add .
$ git commit -m "Initial Commit"
$ git tag 1.0.0

接下来修改前面的Package.swift文件,加入我们的Library:

    dependencies: [
        // Dependencies declare other packages that this package depends on.
        .package(url: "https://github.com/SwiftyJSON/SwiftyJSON.git", from: "4.0.0"),
        .package(url: "./MyLib", from: "1.0.0")    //因为没有推送到远程仓库,所以这里用相对路径,from就是我们刚才打的tag
    ],
    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 which this package depends on.
        .target(
            name: "SPMDemo",
            dependencies: ["SwiftyJSON","MyLib"]),      //添加依赖包名
    ]

然后我们再检验一下依赖包是否可用,编辑main.swift文件:

import SwiftyJSON
import MyLib

//SwiftyJSON
let json = JSON(["name":"Jack", "age": 25])
print(json)

//自定义库
let lib = MyLib()
print(lib)

然后我们再run一次看看:

{
  "name" : "Jack",
  "age" : 25
}
MyLib(text: "Hello, MyLib!”, num: 2)

结果输出了我们自定义的库中结构体的内容,说明我们引入自定义依赖包成功了!

三、如何在iOS工程中使用SPM

除了macOS以外,目前SPM不支持苹果其他平台,所以如果想替换Pods或者Carthage还是谨慎一点,等苹果完全支持iOS以后再切换过来吧。不过我们可以以引入子工程的方式在iOS项目中使用SPM,具体步骤如下:

1、创建新的iOS项目或者使用已存在的项目

2、将该工程初始化为SPM(swift package init)

3、创建一个空的依赖源文件和路径,因为创建xcodeproj文件的时候需要至少一个源文件,这里我在dep-sources路径下创建一个Dependencies.swift文件:

$ mkdir dep-sources
$ cd dep-sources/
$ touch Dependencies.swift

4、修改Package.swift文件,加入依赖库并设置target为新的名字,否则会跟iOS工程的target重名,这里我改为Dependencies,然后在target中设置第3步的路径

// swift-tools-version:4.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "Dependencies",
    products: [
        // Products define the executables and libraries produced by a package, and make them visible to other packages.
        .library(
            name: "Dependencies",
            type: .static,
            targets: ["Dependencies"]),
    ],
    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"),
        .package(url: "https://github.com/Alamofire/Alamofire.git", from: "4.6.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 which this package depends on.
        .target(
            name: "Dependencies",
            dependencies: ["SwiftyJSON","Alamofire"],
            path: "dep-sources" //源文件路径
        )
    ]
)

5、执行swift package generate-xcodeproj命令,这里会生成Dependencies.xcodeproj

$ swift package generate-xcodeproj
Fetching https://github.com/SwiftyJSON/SwiftyJSON.git
Fetching https://github.com/Alamofire/Alamofire.git
Cloning https://github.com/SwiftyJSON/SwiftyJSON.git
Resolving https://github.com/SwiftyJSON/SwiftyJSON.git at 4.0.0
Cloning https://github.com/Alamofire/Alamofire.git
Resolving https://github.com/Alamofire/Alamofire.git at 4.6.0
generated: ./Dependencies.xcodeproj

6、打开iOS工程,将第5步生成的project拖入工程作为sub-project,然后添加依赖的framework就可以了(xcode9经常莫名其妙抽风,建议做完后关闭重新打开工程)

7、验证引入包是否成功

四、可能碰到的问题

1、no such module

$ swift build
Compile Swift Module 'SPMDemo' (1 sources)
/SPMDemo/Sources/SPMDemo/main.swift:2:8: error: no such module 'MyLib'
import MyLib

这有可能是你只加入了依赖包的路径而没有在target中加入模块名称

    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 which this package depends on.
        .target(
            name: "SPMDemo",
            dependencies: ["SwiftyJSON"]),      //缺少“MyLib”
    ]

还有一种情况是更新了依赖包,但是没有构建到.build目录下,这个时候只要把.build文件删除,重新构建一次就可以了。有什么问题欢迎留言讨论!