一句话: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. [日常] Go语言圣经--并发的web爬虫

    两种: crawler.go package main import ( "fmt" "links" //"log" "os&qu ...

  2. [MongoDB] mongodb与php

    windows上安装mongodb的php扩展 下载地址https://s3.amazonaws.com/drivers.mongodb.org/php/index.html 找到对应的php版本的d ...

  3. oracle数据库无法导出空表的问题解决(开始于oracle11g)

    --设置系统参数 alter system set deferred_segment_creation=false; Select 'alter table '||table_name||' allo ...

  4. Lifting the Stone(hdu1115)多边形的重心

    Lifting the Stone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...

  5. Android - fragment Manager

    fragment基本使用: http://www.cnblogs.com/qlky/p/5415679.html Fragmeng优点 Fragment可以使你能够将activity分离成多个可重用的 ...

  6. RocketMQ 消息消费

    消息消费 难点:如何保证消息只消费一次? 消费模式: 1.单一消费模式:一条消息,仅被一个消费者进行消费. 如何进行负载?负载算法有 a.平均分配.b.平均轮询分配.c.一致性hash(不推荐).d. ...

  7. 如何在idea中设置Tomcat虚拟路径

    设置项目的根路径: 设置指定文件的在Tomcat中的虚拟路径: 代码: String fileName = MyFileUtil.getFileName(uploadFileName); File f ...

  8. 初学HTML-4

    img标签:<img src=" "> src——source缩写,告诉img标签需要显示的图片名称 属性:width:宽度   height:高度.若未指定宽和高,则 ...

  9. js-jQuery性能优化(二)

    5.数组方式使用jQuery对象 使用jQuery选择器获取结果是一个jQuery对象.然而,jQuery类库会让你感觉正在使用一个定义了索引和长度的数组.在性能方面,建议使用简单的for或者whil ...

  10. Express 框架

    Express 是一个简洁而灵活的 node.js Web应用框架, 提供了一系列强大特性帮助你创建各种 Web 应用,和丰富的 HTTP 工具.使用 Express 可以快速地搭建一个完整功能的网站 ...