Swift Package Manager,一初探

一句话:Swift Package Manager(swift包管理器,简称:SPM)就是在swift开发中用来替代CocoaPod的;在swift开发中,SPM完全可以替代CocoaPod的功能,并且速度更快,体验更佳;

一、安装SPM

SPM随Xcode 8.x 一起发布,终端上可查看SPM版本:

$ swift package --version
Swift Package Manager - Swift 3.0.0-dev

二、使用SPM创建项目

创建一个可执行项目,如SPMDemo:

$ mkdir SPMDemo    // 创建文件夹
$ cd SPMDemo         // 进入文件夹
$ swift package init --type executable  // 初始化为可执行项目
Creating executable package: SPMDemo
Creating Package.swift
Creating .gitignore
Creating Sources/
Creating Sources/main.swift
Creating Tests/
$ swift package generate-xcodeproj    //生成Xcode工程,可用Xcode打开
generated: ./SPMDemo.xcodeproj
$ swift build     // swift 编译并生成可执行文件
Compile Swift Module 'SPMDemo' (1 sources)
Linking ./.build/debug/SPMDemo
$ ./.build/debug/SPMDemo  // 执行生成的文件
Hello, world!          // 执行效果

三、添加外部模块

我们试着把Alamofire模块添加到SPMDemo中;

1、编辑Package.swift文件,内容如下:

import PackageDescription

let package = Package(
    name: "SPMDemo",
    dependencies: [
         .Package(url: "https://github.com/Alamofire/Alamofire.git", Version(4,2,0))
    ]
)

2、main.swift中引入并使用Alamofire

import Alamofire
print(Alamofire.request("https://httpbin.org/get"))

3、编译并运行

$ swift build
Compile Swift Module 'SPMDemo' (1 sources)
Linking ./.build/debug/SPMDemo
$ ./.build/debug/SPMDemo
GET https://httpbin.org/get

四、更新依赖包

假设我们需要将Alamofire 4.2.0“更新”到4.1.0;

1、编辑Package.swift,将Version(4,2,0)改为Version(4,1,0);

2、更新依赖:

$ swift package update
Cloning https://github.com/Alamofire/Alamofire.git
HEAD is now at c2134d7 Added release notes to the CHANGELOG and bumped the version to 4.1.0.
Resolved version: 4.1.0

可以用 swift package generate-xcodeproj更新一下Xcode工程文件,然后就可以build的运行了

五、创建模块(库)

假设我们需要创建一个BarModule,步骤如下:

1、初始化模块

$ mkdir BarModule 
$ cd BarModule
$ swift package init --type library    // 初始化为一个库
Creating library package: BarModule
Creating Package.swift
Creating .gitignore
Creating Sources/
Creating Sources/BarModule.swift
Creating Tests/
Creating Tests/LinuxMain.swift
Creating Tests/BarModuleTests/
Creating Tests/BarModuleTests/BarModuleTests.swift
$ swift package generate-xcodeproj    // 创建Xcode项目
generated: ./BarModule.xcodeproj

2、编写模块代码

初始化为库时,自动生成了一个文件BarModule.swift,编辑BarModule.swift文件如下:

public struct BarModule {
    public var text = "Hello, Module!"
    public var num: Int
    public init() {
        num = 12
    }
}

3、添加git tag

$ git init
$ git add .
$ git commit -m "Init Commit"
$ git tag 1.0.0      // 添加tag

这个 tag 1.0.0就是我们引用时的版本号

如果把这个BarModule推送到外部,如github上,就是可以通过引入外部引入的方式引入到项目中;

当然,我们还能本地引入模块;

六、本地引入模块

我们将在SPMDemo项目中引入BarModule;

1、编辑SPMDemo的Package.swift文件

import PackageDescription
let package = Package(
    name: "SPMDemo",
    dependencies: [
        .Package(url: "https://github.com/Alamofire/Alamofire.git", Version(4,1,0)),
        .Package(url: "../BarModule", Version(1, 0, 0))  // 添加的代码,版本号就是刚才的tag
        ]
)

2、swift build将BarModule添加到SPMDemo项目中

3、编辑main.swift文件

import Alamofire
import BarModule

print(Alamofire.request("https://httpbin.org/get"))

let bar = BarModule()
print(bar.num)
print(bar.text)

4、编译运行

$ swift build
$ ./.build/debug/SPMDemo
GET https://httpbin.org/get
12
Hello, Module!

七、待解决的问题

以下是笔者尚未解决的问题:

1、如何在Swift的iOS项目中应用Swift Package Manager进行依赖管理?

2、如何用Swift Package Manager发布管理二进制的SDK?

作者:邓国辉

链接:https://www.jianshu.com/p/4caecb22c4bd

来源:简书

简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。