假设我做了一个“酒吧”项目,就像这样:
~ $ mkdir Bar
~ $ cd Bar/
Bar $ swift package init --type library
Bar $ git init .
Bar $ git add .
Bar $ git commit -m "Initial commit"
Bar $ git tag 1.0.0
Bar $ swift build如果我尝试使用第三方依赖项(例如阿拉莫火/阿拉莫火),然后尝试导入该依赖项或( iii) repl中的项目模块,则会得到一个加载错误。
$ swift
1> import Bar
error: repl.swift:1:8: error: no such module Bar'
import Bar
^
1> import Alamofire
error: repl.swift:1:8: error: no such module 'Alamofire'
import Alamofire
^ 如何加载项目+其在Swift中的依赖项?
发布于 2018-02-15 09:55:11
以下是使用Swift 4解决方案的步骤。
创建一个文件夹,比如"TestSPMLibrary":
$ mkdir TestSPMLibrary
$ cd TestSPMLibrary创建一个库包:
$ swift package init --type library在"Package.swift“文件中,添加".dynamic”库类型.
您还可以添加一个依赖项,如Alamofire (您还需要将它添加到目标中)。
我的"Package.swift“示例:
// swift-tools-version:4.0
import PackageDescription
let package = Package(
name: "TestSPMLibrary",
products: [
.library(
name: "TestSPMLibrary",
type: .dynamic,
targets: ["TestSPMLibrary"]),
],
dependencies: [
.package(url: "https://github.com/Alamofire/Alamofire.git", from: "4.0.0"),
],
targets: [
.target(
name: "TestSPMLibrary",
dependencies: ["Alamofire"]),
.testTarget(
name: "TestSPMLibraryTests",
dependencies: ["TestSPMLibrary"]),
]
)在这个库中,您想要与之接口的代码必须声明为公共的(对象需要一个公共初始化程序)。
我的"TestSPMLibrary.swift“示例:
public struct Blah {
public init() {}
public var text = "Hello, World!"
}建立图书馆:
$ swift build使用swift -I .build/debug -L .build/debug -l启动REPL并添加库名。就我而言:
$ swift -I .build/debug -L .build/debug -lTestSPMLibrary在REPL中,您现在可以导入库(及其依赖项):
import TestSPMLibrary
import Alamofire
let x = Blah()
print(x.text)https://stackoverflow.com/questions/48778921
复制相似问题