Xode11 內置了Swift Package Manager(SPM).爲了建立一個新的Swift包,你首先須要下載Xcode 11.git
下載完後,打開Xcode你就能夠開始建立swift package了。 npm
選擇一個名稱,你就擁有了一個全新的Swift框架,能夠在支持Swift的每一個平臺上運行。 json
框架的主要部分是Package.swift文件和Sources文件夾。測試文件夾在存儲測試時也很重要,但我認爲沒有必要告訴你測試的基本要求。swift
這是框架的主要文件,例如帶有Cocoapods的Podspec文件或帶有npm的Package.json。bash
import PackageDescription
let package = Package(
name: "MyFramework",
products: [
// Products define the executables and libraries produced by a package, and make them visible to other packages.
.library(
name: "MyFramework",
targets: ["MyFramework"]),
],
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: "MyFramework",
dependencies: []),
.testTarget(
name: "MyFrameworkTests",
dependencies: ["MyFramework"]),
]
)
複製代碼
您的框架邏輯應存儲在Sources> YOURFRAMEWORKNAME文件夾中。框架
這就完成啦!只需提交併將修改推送到您的git存儲庫便可。如今你能夠在任何Swift項目中使用你的框架。測試