---
title: "[IOS]如何使用Swift Package Manager"
author: "繁星水"
date: "2019-10-18T17:26:21Z"
tags: []
categories: ["移动开发"]
slug: "ios-swift-package-manager"
draft: false
lastmod: "2019-10-18T17:26:21Z"
id: 2509018
url: "https://jameskaron.iteye.com/blog/2509018"
---

视频参考: https://www.youtube.com/watch?v=xu9oeCAS8aA

 

Apple Guide: https://developer.apple.com/documentation/xcode/creating\_a\_swift\_package\_with\_xcode

 

Swift Package Manager (SwiftPM) 是 Apple 推出的一个包管理工具, 用于创建, 使用 Swift 的库, 以及可执行程序的工具.

 

简要步骤如下:

1. Create a Swift Package

File &gt; New &gt; Swift Package

![Screenshot showing a newly created standalone Swift package named ExamplePackage. The Editor area shows the package's manifest file while the Navigator area shows the package's contents and the Utilities area displays the information about the package manifest.](https://docs-assets.developer.apple.com/published/a722ddbfef/fca78bf1-9b3e-41ae-82b8-185b2e871766.png)

 

2. Configure Your Swift Package

 

```
// swift-tools-version:5.1
import PackageDescription

let package = Package(
    name: "MyLibrary",
    platforms: [
        .macOS(.v10_13),
    ],
    products: [
        .library(name: "MyLibrary", targets: ["MyLibrary"]),
    ],
    dependencies: [
        .package(url: "https://url/of/another/package/named/Utility", from: "1.0.0"),
    ],
    targets: [
        .target(name: "MyLibrary", dependencies: ["Utility"]),
        .testTarget(name: "MyLibraryTests", dependencies: ["MyLibrary"]),
    ]
)
```

 

 

3.  Organize Your Code with Swift Packages

Click the + button in the “Frameworks, Libraries, and Embedded Content” section and select the local package’s library to link it into your app target.