如何在 iOS
中使用 Swift Package Manager
?
- 要使用的庫(kù)已經(jīng)適配了
Swift Package Manager
-
Xcode
版本11
在 Swift 項(xiàng)目中使用
- 在
Xcode
導(dǎo)航File
->Swift Packages
->Add Package Dependency...
如上圖: 1. 通過(guò)倉(cāng)庫(kù)連接查找. 2. 通過(guò)登錄git
賬號(hào)查找
后面的步驟都是圖形界面, 很容易看懂就不一一列舉了 - 在
Project
中查看添加修改, 步驟相同.
自建 SPM 庫(kù)
Swift 庫(kù)
這里舉例用 git
.
首先 cmd
到你的項(xiàng)目下, 運(yùn)行 swift package init
, 這時(shí)候會(huì)在目錄下產(chǎn)生一個(gè) Package.swift
, 打開(kāi)它, 注釋是具體說(shuō)明
import PackageDescription
let package = Package(
name: "HYImageBrowser",
// 支持的平臺(tái)和版本
platforms: [
.iOS(.v10)
],
products: [
// Products define the executables and libraries produced by a package, and make them visible to other packages.
.library(
name: "HYImageBrowser",
targets: ["HYImageBrowser"]),
],
// 依賴其他 package
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
.package(url: "https://github.com/onevcat/Kingfisher.git", from: "5.8.3")
],
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: "HYImageBrowser",
dependencies: ["Kingfisher"], // 導(dǎo)入依賴
path: "HYImageBrowser/Sources" // 你要導(dǎo)出的 module, 就是 import 后的代碼就在這部分
)
]
)
到這里一個(gè)基本的 SPM
就可以提交到 git
, 給別人使用, 其他參數(shù)還沒(méi)仔細(xì)研究, 可以閱讀文檔.
現(xiàn)在提交到 git
, 使用者的版本號(hào)對(duì)應(yīng)的是 git tag
, 所以提交后還要打一個(gè) tag
, 比如 1.0.0
, 如下圖 2.0.0
, 就是對(duì)應(yīng)的 tag
, 代表支持的最低版本, 可以自選使用版本的策略.
// 刪除遠(yuǎn)端 tag
git push origin :refs/tags/1.0.0
ObjC 庫(kù)
同樣可以創(chuàng)建 OC
的 SPM
.
與 swift
不同, OC
一定要提供 publicHeadersPath
, 他代表你要導(dǎo)出給別人用的文件夾, 如果不提供 publicHeadersPath
, path:
文件夾下就必須包含一個(gè) include
文件夾, 然后把所有要導(dǎo)出的 .h
放入到其中.
如下代碼:
.target(
name: "SPM_OCTest",
path: "Sources",
publicHeadersPath: "../Sources"
)
path:
代表你的 module,
publicHeadersPath:
字面意思也可以讀懂, 就是提供的接口文件.
如果沒(méi)有 publicHeadersPath:
參數(shù), 默認(rèn)的文件夾就是 path/include
, 上面例子中的文件夾就應(yīng)該是Sources
文件夾下必須有一個(gè) include
文件夾用來(lái)導(dǎo)出接口文件. 上面例子, 我提供的 publicHeadersPath: "../Sources"
, 是把 Sources
文件夾下的所有文件都對(duì)外提供.
import PackageDescription
let package = Package(
name: "SPM_OCTest",
platforms: [
.iOS(.v9)
],
products: [
// Products define the executables and libraries produced by a package, and make them visible to other packages.
.library(
name: "SPM_OCTest",
type: .dynamic,
targets: ["SPM_OCTest"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.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: "SPM_OCTest",
path: "Sources",
publicHeadersPath: "../Sources"
),
.testTarget(
name: "SPM_OCTestTests",
dependencies: ["SPM_OCTest"]),
]
)
End
示例代碼 中的 SwiftPMTest
和 SPM_OCTest
目前 iOS 不支持在 OC 項(xiàng)目中使用 SPM
目前 iOS 不支持在 OC 項(xiàng)目中使用 SPM
reference: Creating Swift Packages
reference: Apple
reference: Apple