一句话: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
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

最新文章

  1. [整理]AngularJS移动端开发遇到的问题
  2. Android开发 Failed to install *.apk on device 'emulator-5554': EOF
  3. Gunicorn + Django 部署
  4. 使用linq 对 DataTable 中的数据进行 查询 与 分类求合
  5. BZOJ 1833 ZJOI2010 count 数字计数 数位DP
  6. HDU 5811 Colosseo
  7. parquet列式文件实战
  8. Fiddler使用简单介绍
  9. linux centos6.5安装KVM
  10. (59)Wangdao.com第十天_JavaScript 对象在 栈和堆
  11. itemKNN发展史----推荐系统的三篇重要的论文解读
  12. CentOS的el5, el6, el7代表什么
  13. Luogu P1892 P1525 团伙 关押罪犯
  14. js 錯誤
  15. UI5-学习篇-14-基于BSP应用部署Fiori Launchpad
  16. no-referrer-when-downgrade什么意思
  17. Daily Scrumming* 2015.10.26(Day 7)
  18. angular-file-upload
  19. SQL HAVING用法详解
  20. ASP.NET MVC中,前台DropDownList传值给后台。

热门文章

  1. css布局记录之双飞翼布局、圣杯布局
  2. JFace TableViewer性能改善 -- 使用VirtualTable
  3. 了解java虚拟机—垃圾回收算法(5)
  4. LINQ to Objects系列(3)深入理解Lambda表达式
  5. Vue: 生命周期, VueRouter
  6. cf711D. Directed Roads(环)
  7. git基础使用——TortoiseGit
  8. JavaScript 事件处理机制
  9. 【代码笔记】iOS-二维码
  10. 高性能JavaScript(快速响应的用户界面)