xcode 无法使用 Swift 4 包管理器导入包

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45023201/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 10:07:06  来源:igfitidea点击:

Can't import packages using Swift 4 Package Manager

swiftxcodemacospackage-managersswift4

提问by Timka

Trying to test Swift 4 using Xcode-beta (v9) on my machine and having issues with importing packages into a test project:

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

  • Initiated project using swift package init --type executable
  • Changed Package.swiftand added 2 projects to try out:
  • 启动项目使用 swift package init --type executable
  • 更改Package.swift并添加了 2 个项目进行试用:

Package.swift

包.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: []),
    ]
)
  • Run swift build && swift package generate-xcodeproj
  • When I open project in Xcode-beta(v9) and try to import Kitura or Alamofire, I'm getting No such module Kitura/Alamofireerror message
  • Running swift buildin terminal produces the following error:
  • swift build && swift package generate-xcodeproj
  • 当我在 Xcode-beta(v9) 中打开项目并尝试导入 Kitura 或 Alamofire 时,我收到No such module Kitura/Alamofire错误消息
  • swift build在终端中运行会产生以下错误:

Compile Swift Module 'investprosto' (1 sources) /Users/username/Projects/sampleproject/Sources/sampleproject/main.swift:1:8: error: no such module 'Kitura' import Kitura ^ error: terminated(1): /Applications/Xcode- beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift- build-tool -f /Users/username/Projects/sampleproject/.build/debug.yaml

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

Dependenciesvirtual folder contains the directories with the same package names, but they are empty. However, .build\checkoutsand .build\repositoriescontain packages folders and corresponding files.

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

Is there something I'm missing in my system's configuration?

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

回答by Timka

Turns out I had to also include the dependencies into the .targetof the Package.swift:

结果我还必须将依赖项包含到.targetPackage.swift 中:

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

and build the project again.

并再次构建项目。

回答by Krishnadas PC

Even though you need to add the targetinto the Package.swiftsometimes it's not enough. I have fixed the issue by deleting the .builddir and the file Package.resolved, then running swift buildor build from Xcode. Build command was not fetching packages as it was already in resolvedfile but if you delete the .builddir it becomes meaningless. You can verify it by checking the Dependenciesdir from Xcodeit will be empty if there is error like No such package/module

即使您需要将其添加targetPackage.swift有时也不够。我通过删除.build目录和文件Package.resolved,然后swift buildXcode运行或构建来解决这个问题。构建命令没有获取包,因为它已经在resolved文件中,但如果删除.build目录,它就变得毫无意义。您可以通过检查Xcode 中Dependencies目录来验证它,如果出现类似错误,它将为空No such package/module