【问题标题】:Xcode giving "Cannot find type 'TYPE' in scope" errors with Swift PackageXcode 使用 Swift 包给出“在范围内找不到类型 'TYPE'”错误
【发布时间】:2021-04-03 17:22:01
【问题描述】:

多年来,我一直在使用服务器端 Swift 进行服务器端开发,并使用 Xcode 作为方便的编辑工具。现在 Xcode 已经支持 Swift 包,情况总体上有所改善。我没有针对任何传统的 Apple 硬件,因此这可能是我的问题的根源,但鉴于这个问题最近才开始出现,我正在报告它以防其他人也遇到它。

使用我的一个包裹: http://github.com/SyncServerII/ServerDropboxAccount.git 我不能再用 Xcode 构建它了。

我的意图主要是编辑和消除语法错误——即,我使用 Xcode 作为编辑器。在某些情况下,使用这些服务器端包,我可以在 Xcode 中运行单元测试。在某些情况下,我必须在我的 Ubuntu 目标平台上运行测试。

就在过去的几天里,我不能再在 Xcode 中构建这个特定的包,也不能在 Mac OS 上的命令行中构建。

我收到这样的错误:

/Users/chris/Desktop/NewSyncServer/ServerDropboxAccount/Sources/ServerDropboxAccount/DropboxCreds+CloudStorage.swift:140:37: Cannot find type 'APICallResult' in scope
/Users/chris/Desktop/NewSyncServer/ServerDropboxAccount/Sources/ServerDropboxAccount/DropboxCreds.swift:16:29: Cannot find type 'AccountAPICall' in scope
/Users/chris/Desktop/NewSyncServer/ServerDropboxAccount/Sources/ServerDropboxAccount/DropboxCreds.swift:16:45: Cannot find type 'Account' in scope
/Users/chris/Desktop/NewSyncServer/ServerDropboxAccount/Sources/ServerDropboxAccount/DropboxCreds+CloudStorage.swift:49:14: Value of type 'DropboxCreds' has no member 'apiCall'
/Users/chris/Desktop/NewSyncServer/ServerDropboxAccount/Sources/ServerDropboxAccount/DropboxCreds+CloudStorage.swift:49:111: Cannot infer contextual base in reference to member 'string'
/Users/chris/Desktop/NewSyncServer/ServerDropboxAccount/Sources/ServerDropboxAccount/DropboxCreds+CloudStorage.swift:49:147: Cannot infer contextual base in reference to member 'json'
/Users/chris/Desktop/NewSyncServer/ServerDropboxAccount/Sources/ServerDropboxAccount/DropboxCreds+CloudStorage.swift:106:14: Value of type 'DropboxCreds' has no member 'apiCall'
/Users/chris/Desktop/NewSyncServer/ServerDropboxAccount/Sources/ServerDropboxAccount/DropboxCreds+CloudStorage.swift:106:133: Cannot infer contextual base in reference to member 'data'
/Users/chris/Desktop/NewSyncServer/ServerDropboxAccount/Sources/ServerDropboxAccount/DropboxCreds+CloudStorage.swift:106:167: Cannot infer contextual base in reference to member 'json'
/Users/chris/Desktop/NewSyncServer/ServerDropboxAccount/Sources/ServerDropboxAccount/DropboxCreds+CloudStorage.swift:106:190: Unable to infer type of a closure parameter 'apiResult' in the current context
/Users/chris/Desktop/NewSyncServer/ServerDropboxAccount/Sources/ServerDropboxAccount/DropboxCreds+CloudStorage.swift:106:201: Unable to infer type of a closure parameter 'statusCode' in the current context

当我在 Mac OS 的命令行中使用 swift build 时,我得到了同样的错误。但是,当我在 Ubuntu 上使用 swift build 时,我没有收到任何错误——包构建得很干净。

在 Mac 操作系统上:

MacBook-Pro-4:ServerDropboxAccount chris$ swift --version
Apple Swift version 5.3.2 (swiftlang-1200.0.45 clang-1200.0.32.28)
Target: x86_64-apple-darwin19.6.0

在 Ubuntu 上:

root@7b763a0a0a3f:~/Apps/ServerDropboxAccount# swift --version
Swift version 5.3.1 (swift-5.3.1-RELEASE)
Target: x86_64-unknown-linux-gnu

这可能是一个边缘用例,但在我完全放弃使用 Xcode 之前,我想了解为什么会发生这种情况。想法受到赞赏。谢谢!

我在这个问题中包含了Kitura 的标签,因为我的服务器是基于 Kitura 的。而且因为 Kitura 最近经历了一些转变,从 IBM 转向社区支持。

【问题讨论】:

    标签: swift xcode12 kitura server-side-swift


    【解决方案1】:

    嗯,我现在觉得很愚蠢。我在依赖库中的一个文件中有一个条件,它排除了一些代码。

    #if os(Linux) || SERVER
    
    // Code
    
    #endif
    

    我将留下这个问题只是为了显示修复。我需要将 SERVER 的定义添加到该依赖库的 Package.swift 中:

            .target(
                name: "ServerAccount",
                dependencies: [
                    "ServerShared",
                    // For new condition feature, see https://forums.swift.org/t/package-manager-conditional-target-dependencies/31306/26
                    .product(name: "Kitura", package: "Kitura", condition: .when(platforms: [.linux, .macOS])),
                    .product(name: "HeliumLogger", package: "HeliumLogger", condition: .when(platforms: [.linux, .macOS])),
                    .product(name: "Credentials", package: "Kitura-Credentials", condition: .when(platforms: [.linux, .macOS])),
                ],
                swiftSettings: [
                    // So I can do basic development and editing with this on Mac OS. Otherwise if some dependent library uses this it will not get Account related code. See Account.swift.
                    .define("SERVER", .when(platforms: [.macOS], configuration: .debug)),
                ]),
    

    哎呀。 :)。

    【讨论】:

      猜你喜欢
      • 2021-08-19
      • 2021-09-17
      • 2021-12-06
      • 2021-11-18
      • 2023-02-21
      • 1970-01-01
      • 2022-11-27
      • 1970-01-01
      • 2021-02-02
      相关资源
      最近更新 更多