有关SPM的文档非常贫乏,关于它的大多数文章都是在SPM开始时发表的。
我已经在Swift中实现了一个算法(匈牙利算法),并期待着将它作为一个库发布在Github。我不得不在这个项目中第一次使用SPM来解决另一个依赖,一旦它开始工作,它是完美的。
现在,我无法从另一个项目中使用我的库。我已经决定开始一个新的新的吉特回购,因为我无法让以前的工作。
库名为Hume,它包含在Hume.swift文件中,该文件定义休谟类。
我所经历的步骤如下:
swift package init --type libraryswift package init --type executableswift build此时,快速复制库的回购并没有问题地编译它(因为main.swift只包含一个Hello )。
swift package generate-xcodeproj当我打开项目并试图导入我的库时,模块名似乎被识别了,但是当我试图声明一个对象时,它说我不能声明一个类型为模块的变量。
这是库中的Package.swift文件:
// swift-tools-version:4.0
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "Hume",
products: [
.library(
name: "Hume",
targets: ["Hume"]),
],
dependencies: [
.package(url: "https://github.com/aleph7/Upsurge.git", from: "0.10.2"),
],
targets: [
.target(
name: "Hume",
dependencies: ["Upsurge"]),
.testTarget(
name: "HumeTests",
dependencies: ["Hume"]),
]
)库只有一个具有此样式的文件:
import Upsurge
class Hume {
// Attributes
....
init(matriz:[[Double]]){
....
}
public func resuelve() -> (Double, [(Int, Int)]){
....
}
}这是虚拟可执行文件中的Package.swift:
// swift-tools-version:4.0
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "aa",
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
.package(url: "https://github.com/Jasagredo/Hume.git", from: "0.1.1"),
],
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: "aa",
dependencies: ["Hume"]),
]
)建造时的输出:
~/Desktop/aa swift build
Fetching https://github.com/Jasagredo/Hume.git
Fetching https://github.com/aleph7/Upsurge.git
Cloning https://github.com/Jasagredo/Hume.git
Resolving https://github.com/Jasagredo/Hume.git at 0.1.1
Cloning https://github.com/aleph7/Upsurge.git
Resolving https://github.com/aleph7/Upsurge.git at 0.10.2
Compile Swift Module 'Upsurge' (30 sources)
Compile Swift Module 'Hume' (1 sources)
Compile Swift Module 'aa' (1 sources)
Linking ./.build/x86_64-apple-macosx10.10/debug/aa
~/Desktop/aa 但是,当我在虚拟可执行文件中编辑main.swift时,我会发现以下错误:
import Hume
var a = Hume(matriz: [[1,1],[1,1]]) //Cannot call value of non-function type 'module<Hume>'而且,Xcode不会自动建议我上休谟课。我只是不知道我做错了什么。
任何帮助都是非常感谢的。
发布于 2018-04-29 12:25:32
我终于把事情做好了。问题是类(以及它的init方法)没有声明为public。配置的其余部分是正确的。
https://stackoverflow.com/questions/50082642
复制相似问题