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

Swift Package Manager(一)初探的更多相关文章

  1. 使用 Swift Package Manager 集成依赖库

      本文首发于 Ficow Shen's Blog,原文地址: 使用 Swift Package Manager 集成依赖库.   内容概览 前言 添加依赖包 在项目中使用依赖 管理已导入的依赖 在团 ...

  2. 你需要知道的包管理器(Package Manager)

    最近我花了一点时间关注了在不同系统之中所用到的包管理器(Package Manager) .最开始的时候,我是在使用Linux操作系统时,对这种工具以及它背后的想法深深迷恋住了:这真是自由的软件世界. ...

  3. 解决VS2015启动时Package manager console崩溃的问题 - Windows PowerShell updated your execution policy successfully, but the setting is overridden by a policy defined at a more specific scope

    安装VS2015,启动以后,Package manager console崩溃,错误信息如下: Windows PowerShell updated your execution policy suc ...

  4. Visual Studio 2015 新建MVC项目 Package Manager Console不能使用 (HRESULT: 0x80131500)

    Visual studio 2015 突然新建不了MVC项目,报出错误: HRESULT: 0x80131500 在折腾了很长时间,最后在Github上看到这样一个贴 地址:https://githu ...

  5. Error: Could not access the Package Manager. Is the system running?

    最近在搭建cordova,android 开发环境,安装android studio之后创建一个demo之后,运行想看一下效果,在运行过程中创建一个虚拟机(arm)的,等了有1分钟左右,再次运行程序, ...

  6. Visual Studio 2010 更新NuGet Package Manager出错解决办法

    在Visual Studio 2010的扩展管理器中发现NuGet Package Manger有最新版本更新提示,选择更新安装提示以下错误信息: 2013/4/25 1:11:48 - Micros ...

  7. Getting and installing the PEAR package manager

    Windows After you have downloaded and installed PHP, you have to manually execute the batch file loc ...

  8. RPM是RedHat Package Manager(RedHat软件包管理工具)

    RPM是RedHat Package Manager(RedHat软件包管理工具)类似Windows里面的“添加/删除程序” rpm 执行安装包二进制包(Binary)以及源代码包(Source)两种 ...

  9. installation - How to install Synaptic Package Manager? - Ask Ubuntu

    installation - How to install Synaptic Package Manager? - Ask Ubuntu How to install Synaptic Package ...

随机推荐

  1. WPF备忘录(1)有笑脸,有Popup

    1.画个笑脸给大家娱乐一下: <Canvas Width="200" Height="180" VerticalAlignment="Cente ...

  2. JS DOM操作(五) Window.docunment对象——操作元素

    定位: var a = document.getElementByIt( "id" ) 同辈元素 var b = a.nextSibling;            -- 找 a ...

  3. EF只更新变化的字段

    摘要 在使用EF的时候,由于表字段较多,所以在更新的时候,想要只更新变化的字段,有没有办法呢? 解决办法 代码片段 public async Task<int> UpdateAsync(T ...

  4. Book Contents Reviews Notes Errata Articles Talks Downloads Resources Code Formatter Cover of C# in Depth Order now (3rd edition) Implementing the Singleton Pattern in C#

    原文链接地址: http://csharpindepth.com/Articles/General/Singleton.aspx#unsafe Implementing the Singleton P ...

  5. Oracle11g自带的SQL_developer无法打开

    在安装完Oracle Database 11g Release 2数据库,想试一下Oracle自带的SQL DeveloperW工具,在操作系统菜单的所有程序中找到SQL Developer如下所示, ...

  6. 【10-2】复杂业务状态的处理(从状态者模式到FSM)

    一.概述 我们平常在开发业务模块时,经常会遇到比较复杂的状态转换.比如说用户可能有新注册.实名认证中.已实名认证.禁用等状态,支付可能有等待支付.支付中.已支付等状态.OA系统里的状态处理就更多了. ...

  7. BZOJ2957: 楼房重建(分块)

    题意 题目链接 Sol 自己YY出了一个\(n \sqrt{n} \log n\)的辣鸡做法没想到还能过.. 可以直接对序列分块,我们记第\(i\)个位置的值为\(a[i] = \frac{H_i}{ ...

  8. 关于ajax 传递的参数

    ajax 发送的数据,默认都是字符串,不能直接传递list(列表),或者dict(字典). 若要 传递list(列表),或者dict(字典),需要进行一些操作. list 需要进行列表序列化,在aja ...

  9. 【Redis】Redis学习(三) Redis 主从模式详解

    不管任何程序,只运行一个实例都是不可靠的,一旦因为网络原因导致所在机器不可达,或者所在服务器挂掉,那么这个程序将不能对外提供服务了,Redis也是一样的.不过Redis的主从并不是解决这个问题的,一些 ...

  10. Oracle EBS OM 登记订单

    DECLARE l_header_rec OE_ORDER_PUB.Header_Rec_Type; l_line_tbl OE_ORDER_PUB.Line_Tbl_Type; l_action_r ...