代码之家  ›  专栏  ›  技术社区  ›  Chris Prince

Xcode在Swift包中出现“在范围中找不到类型”错误

  •  0
  • Chris Prince  · 技术社区  · 5 年前

    我使用服务器端Swift进行服务器端开发已经有几年了,使用Xcode作为一个方便的编辑工具。现在Xcode已经支持Swift包,情况已经得到了普遍的改善。我不是针对任何传统的苹果硬件,所以这可能是我的问题的根源,但鉴于这个问题最近才开始出现,我报告它的情况下,其他人也遇到它。

    http://github.com/SyncServerII/ServerDropboxAccount.git 我再也不能用Xcode构建它了。

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

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

    我会遇到这样的错误:

    /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
    

    当我使用 swift build 在Mac OS上的命令行。但是,当我使用 在Ubuntu上,我没有发现任何错误——包的构建非常干净。

    在Mac OS上:

    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
    

    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最近经历了一些从IBM到社区支持的转变。

    0 回复  |  直到 5 年前
        1
  •  1
  •   Chris Prince    5 年前

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

    #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)),
                ]),