无法使用Swift 4 Package Manager导入包

时间:2017-07-10 23:38:44

标签: swift xcode macos package-managers swift4

尝试在我的机器上使用Xcode-beta(v9)测试Swift 4,并将包导入测试项目时遇到问题:

  • 使用swift package init --type executable
  • 启动项目
  • 更改了Package.swift并添加了2个项目试用:

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: "sampleproject",
    dependencies: [
        // Dependencies declare other packages that this package depends on.
        // .package(url: /* package url */, from: "1.0.0"),
        .package(url: "https://github.com/IBM-Swift/Kitura.git", from: "1.7.6"),
        .package(url: "https://github.com/Alamofire/Alamofire.git", from: "4.5.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: "sampleproject",
            dependencies: []),
    ]
)
  • 运行swift build && swift package generate-xcodeproj
  • 当我在Xcode-beta(v9)中打开项目并尝试导入Kitura或Alamofire时,我收到No such module Kitura/Alamofire错误消息
  • 在终端中运行swift build会产生以下错误:
  

编译Swift模块'investprosto'(1个来源)   /Users/username/Projects/sampleproject/Sources/sampleproject/main.swift:1:8:   错误:没有这样的模块'Kitura'导入Kitura          ^错误:终止(1):/ Applications / Xcode- beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-   build-tool -f /Users/username/Projects/sampleproject/.build/debug.yaml

依赖关系虚拟文件夹包含具有相同包名称的目录,但它们为空。但是,.build\checkouts.build\repositories包含包文件夹和相应的文件。

我的系统配置中是否缺少某些内容?

2 个答案:

答案 0 :(得分:50)

原来我还必须将依赖项包含在Package.swift的.target中:

.target(named: "sampleproject", dependencies: ["Kitura", "Alamofire"])

再次构建项目。

答案 1 :(得分:0)

即使您需要将target添加到Package.swift中,有时还是不够的。我已解决此问题,方法是删除.build目录和文件Package.resolved,然后运行swift build或从 Xcode 构建。生成命令未获取软件包,因为它已经在resolved文件中,但是如果删除.build目录,它将变得毫无意义。您可以通过检查 Xcode 中的Dependencies目录来验证它,如果出现类似No such package/module

的错误,该目录将为空