iOS开发Swift 基础

Swift Package Manager

2019-10-24  本文已影响0人  幸运者_Lucky

如何在 iOS 中使用 Swift Package Manager?

  1. 要使用的库已经适配了 Swift Package Manager
  2. Xcode 版本 11

在 Swift 项目中使用

  1. Xcode 导航 File -> Swift Packages -> Add Package Dependency...


    如上图: 1. 通过仓库连接查找. 2. 通过登录 git 账号查找
    后面的步骤都是图形界面, 很容易看懂就不一一列举了
  2. Project 中查看添加修改, 步骤相同.

自建 SPM 库

Swift 库

这里举例用 git.
首先 cmd 到你的项目下, 运行 swift package init, 这时候会在目录下产生一个 Package.swift, 打开它, 注释是具体说明

import PackageDescription

let package = Package(
    name: "HYImageBrowser",
    // 支持的平台和版本
    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"], // 导入依赖
            path: "HYImageBrowser/Sources" // 你要导出的 module, 就是 import 后的代码就在这部分
        ) 
    ]
)

到这里一个基本的 SPM 就可以提交到 git, 给别人使用, 其他参数还没仔细研究, 可以阅读文档.
现在提交到 git, 使用者的版本号对应的是 git tag, 所以提交后还要打一个 tag, 比如 1.0.0, 如下图 2.0.0, 就是对应的 tag, 代表支持的最低版本, 可以自选使用版本的策略.

// 删除远端 tag
git push origin :refs/tags/1.0.0
ObjC 库

同样可以创建 OCSPM.
swift不同, OC 一定要提供 publicHeadersPath, 他代表你要导出给别人用的文件夹, 如果不提供 publicHeadersPath, path: 文件夹下就必须包含一个 include 文件夹, 然后把所有要导出的 .h 放入到其中.
如下代码:

.target(
            name: "SPM_OCTest",
            path: "Sources",
            publicHeadersPath: "../Sources"
        )

path: 代表你的 module,
publicHeadersPath: 字面意思也可以读懂, 就是提供的接口文件.
如果没有 publicHeadersPath: 参数, 默认的文件夹就是 path/include, 上面例子中的文件夹就应该是Sources 文件夹下必须有一个 include 文件夹用来导出接口文件. 上面例子, 我提供的 publicHeadersPath: "../Sources", 是把 Sources 文件夹下的所有文件都对外提供.

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

示例代码 中的 SwiftPMTestSPM_OCTest

目前 iOS 不支持在 OC 项目中使用 SPM
目前 iOS 不支持在 OC 项目中使用 SPM

reference: Creating Swift Packages

上一篇下一篇

猜你喜欢

热点阅读