【问题标题】:Swift compiler is confusing version 2 and 3Swift 编译器混淆了版本 2 和 3
【发布时间】:2016-07-01 16:49:14
【问题描述】:

我在一个项目中安装了一个 cocoapod 并在其上运行转换器以将其转换为 Swift 3。

问题是编译器认为我使用的是 Swift 2,并在正确的 Swift 3 语法上抛出错误(高达 129 个错误)。例如:

internal enum Error: ErrorProtocol {
    case noCentralManagerSet
    case busy
    case interrupted
}

抛出use of undeclared type 'ErrorProtocol'

怎么了?

我编写的 Swift 3 代码不会引发编译器错误。

这是xcodebuild -showsdks的输出:

2016-07-01 14:16:05.850 xcodebuild[30824:2848916] [MT] PluginLoading: Required plug-in compatibility UUID 1637F4D5-0B27-416B-A78D-498965D64877 for plug-in at path '~/Library/Application Support/Developer/Shared/Xcode/Plug-ins/WebDevSupport.xcplugin' not present in DVTPlugInCompatibilityUUIDs
2016-07-01 14:16:05.864 xcodebuild[30824:2848916] [MT] PluginLoading: Required plug-in compatibility UUID 1637F4D5-0B27-416B-A78D-498965D64877 for plug-in at path '~/Library/Application Support/Developer/Shared/Xcode/Plug-ins/WebDevelopment.xcplugin' not present in DVTPlugInCompatibilityUUIDs
2016-07-01 14:16:05.911 xcodebuild[30824:2848916] [MT] PluginLoading: Required plug-in compatibility UUID 1637F4D5-0B27-416B-A78D-498965D64877 for plug-in at path '~/Library/Application Support/Developer/Shared/Xcode/Plug-ins/WakaTime.xcplugin' not present in DVTPlugInCompatibilityUUIDs
2016-07-01 14:16:05.944 xcodebuild[30824:2848916] [MT] PluginLoading: Required plug-in compatibility UUID 1637F4D5-0B27-416B-A78D-498965D64877 for plug-in at path '~/Library/Application Support/Developer/Shared/Xcode/Plug-ins/Rayrolling.xcplugin' not present in DVTPlugInCompatibilityUUIDs
2016-07-01 14:16:05.967 xcodebuild[30824:2848916] [MT] PluginLoading: Required plug-in compatibility UUID 1637F4D5-0B27-416B-A78D-498965D64877 for plug-in at path '~/Library/Application Support/Developer/Shared/Xcode/Plug-ins/CocoaPods.xcplugin' not present in DVTPlugInCompatibilityUUIDs
2016-07-01 14:16:05.997 xcodebuild[30824:2848916] [MT] PluginLoading: Required plug-in compatibility UUID 1637F4D5-0B27-416B-A78D-498965D64877 for plug-in at path '~/Library/Application Support/Developer/Shared/Xcode/Plug-ins/Alcatraz.xcplugin' not present in DVTPlugInCompatibilityUUIDs
OS X SDKs:
    OS X 10.12                      -sdk macosx10.12

iOS SDKs:
    iOS 10.0                        -sdk iphoneos10.0

iOS Simulator SDKs:
    Simulator - iOS 10.0            -sdk iphonesimulator10.0

tvOS SDKs:
    tvOS 10.0                       -sdk appletvos10.0

tvOS Simulator SDKs:
    Simulator - tvOS 10.0           -sdk appletvsimulator10.0

watchOS SDKs:
    watchOS 3.0                     -sdk watchos3.0

watchOS Simulator SDKs:
    Simulator - watchOS 3.0         -sdk watchsimulator3.0

【问题讨论】:

    标签: swift3 swift2 xcode8


    【解决方案1】:

    您可以为每个目标设置 Swift 版本:

    您的项目可能设置为Yes

    SDK 不参与编译过程。

    【讨论】:

      【解决方案2】:

      我想我知道问题出在哪里。您已将所有文件从Swift2.0 直接转换为Swift3。它不会那样工作。我建议您删除 pod 并克隆 thisSwift3 分支(我相信您正在使用)并将源目录复制到您的项目中,因为它没有说明哪个版本回购是swift-3 可用。它应该可以解决您的问题。 :)

      【讨论】:

      • 命令xcodebuild -showsdks 应该告诉我什么?而且我只运行 Xcode 8。
      • 我告诉过你。它将显示 Xcode 当前使用的 sdks 列表。你根本没有 Xcode 7?
      • 是的,我删除了 Xcode 7,因为我在硬潜水中空间不足。我将命令的输出添加到问题中。
      • 你能告诉我你用的是哪个pod吗?
      【解决方案3】:

      有两件事可以帮助我处理此类案件。

      因为 Xcode 会自动打开以前从未关闭的项目,如果您在 Xcode 7 中打开项目 A,然后打开 Xcode 8 以开始新的操作,Xcode 8 也会打开同一个项目 A。所以您实际上必须在 Xcode 中关闭项目 A 8 否则你会得到大量的错误,你可能会认为你在 Xcode 7 中工作,因为项目 A 是开放的。此时项目 A 在 Xcode 7 和 Xcode 8 中打开。

      我还将我的项目文件夹命名为 projectName - Swift 2,然后复制它并将其命名为 `projectName - Swift 3。

      当然我们有版本控制,但为了安全起见,我喜欢为 Swift 2 / 3 设置两个单独的文件夹,因为在两个版本的 Xcode 中打开同一个项目会变得很疯狂。

      由于您还必须告诉终端使用单独的目录进行 pod 安装,因此更安全,因为没有重叠。

      【讨论】:

        猜你喜欢
        • 2013-01-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-18
        • 1970-01-01
        • 2017-12-07
        • 1970-01-01
        • 2020-01-31
        相关资源
        最近更新 更多