ios 如何在 Xcode 9 上构建 Swift 3 项目?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44389591/
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-08-31 09:11:38  来源:igfitidea点击:

How to build Swift 3 project on Xcode 9?

iosxcodeswift3swift4xcode9

提问by Exey Panteleev

Xcode 9 Release Notessays that Swift 3 compilation is supported:

Xcode 9 Release Notes说支持 Swift 3 编译:

One compiler for Swift 4 and Swift 3, Swift 4 and Swift 3 targets can be compiled together in the same project.

Swift 4 和 Swift 3 的一个编译器,Swift 4 和 Swift 3 目标可以在同一个项目中一起编译。

I have installed Xcode 9 beta. But when I press Build it shows bunch of errors. Are some special preparations need to do when use Swift 3 in Xcode 9?

我已经安装了 Xcode 9 测试版。但是当我按 Build 时,它会显示一堆错误。在 Xcode 9 中使用 Swift 3 时是否需要做一些特殊的准备?

UPD: Module compiled with Swift 3.1 cannot be imported in Swift 4.0

更新: 无法在 Swift 4.0 中导入使用 Swift 3.1 编译的模块

Module compiled with Swift 3.1 cannot be imported in Swift 4.0

无法在 Swift 4.0 中导入使用 Swift 3.1 编译的模块

回答by vadian

Select the target, goto Build Settings > Swift Language Version:

选择目标,转到Build Settings > Swift Language Version

enter image description here

在此处输入图片说明

回答by Jeremie D

All the above answers are answering the wrong question. When using Xcode 9, you are using the Swift 4 compiler (even if you are using it to compile Swift 3 code) to fix this, recompile using the appropriate compiler/Xcode.

以上所有答案都在回答错误的问题。使用 Xcode 9 时,您使用的是 Swift 4 编译器(即使您使用它来编译 Swift 3 代码)来解决此问题,请使用适当的编译器/Xcode 重新编译。

If you are trying to do this via commandline, you can use sudo xcode-select -switchto switch between xcode versions. Good luck!

如果您尝试通过命令行执行此操作,则可以使用sudo xcode-select -switch在 xcode 版本之间切换。祝你好运!

回答by Shubham Mishra

Update for Xcode 10.1 and Swift 4.2

Xcode 10.1 和 Swift 4.2 的更新

Swift 3, 4, and 4.2 targets can coexist and link together.

Swift 3、4 和 4.2 目标可以共存并链接在一起。

You decide when and if you'd like to migrate on a per-target basis when it makes sense for your project. While migrating to Swift 4.2 is definitely encouraged, it's not an all-or-nothing process, as Swift 3, 4, and 4.2 targets can coexist and link together.

当对您的项目有意义时,您可以决定何时以及是否要基于每个目标进行迁移。虽然肯定鼓励迁移到 Swift 4.2,但这不是一个全有或全无的过程,因为 Swift 3、4 和 4.2 目标可以共存并链接在一起。

Different cocoapod and different project version can also coexist.

不同的cocoapod和不同的项目版本也可以共存。

You wish to have different cocoapod version as compared to your project version then you should make following changes at the end of your pod file:

与您的项目版本相比,您希望拥有不同的 cocoapod 版本,那么您应该在 pod 文件的末尾进行以下更改:

post_install do |installer|
    print "Setting the default SWIFT_VERSION to 4.2\n"
    installer.pods_project.build_configurations.each do |config|
        config.build_settings['SWIFT_VERSION'] = '4.2'
    end

    installer.pods_project.targets.each do |target|
        if ['SomeTarget-iOS', 'SomeTarget-watchOS'].include? "#{target}"
            print "Setting #{target}'s SWIFT_VERSION to 3.0\n"
            target.build_configurations.each do |config|
                config.build_settings['SWIFT_VERSION'] = '3.0'
            end
        else
            print "Setting #{target}'s SWIFT_VERSION to Undefined (Xcode will automatically resolve)\n"
            target.build_configurations.each do |config|
                config.build_settings.delete('SWIFT_VERSION')
            end
        end
    end
end

Sometimes even after you run pod update your xcode forcefully update your cocoapod's project version, then in that case select cocoapod pod project and change the swift version in the build settings.

有时即使你运行 pod update 你的 xcode 强制更新你的 cocoapod 的项目版本,然后在这种情况下选择 cocoapod pod 项目并在构建设置中更改 swift 版本。

回答by Jeni Khant

I had same issue - (I installed pod which swift version was 3x and my project's swift version was Swift 4. So I got so many compiler errors. Later I changed the Swift version form 4 to 3.2 and tried to build project, again I got compiler errors. I think that happen because of I run pod install while my project was in Swift 4.)

我有同样的问题 - (我安装了 pod,它的 swift 版本是 3x,我的项目的 swift 版本是 Swift 4。所以我遇到了很多编译器错误。后来我将 Swift 版本的 4 版本更改为 3.2 并尝试构建项目,我再次得到编译器错误。我认为这是因为我的项目在 Swift 4 中运行 pod install。)

  • First you need to change the "Swift language version" from build settings.
  • If you have installed any pod then changing only "Swift Language Version" won't help. You may need to run pod install for the project.
  • 首先,您需要从构建设置中更改“Swift 语言版本”。
  • 如果您安装了任何 pod,那么仅更改“Swift 语言版本”将无济于事。您可能需要为项目运行 pod install。

回答by AtomicBoolean

Xcode 9 needs the framework to be built with Swift 3.2 or higher. Go to https://github.com/emaloney/CleanroomLoggerand click Download ZIP. Open the xcodeproj in Xcode 9. Press ?B. CleanroomLogger.framework turns from red to black. Drag the framework into your project. See related question: Realm issue with Swift 3.1

Xcode 9 需要使用 Swift 3.2 或更高版本构建框架。转到https://github.com/emaloney/CleanroomLogger并单击下载 ZIP。在 Xcode 9 中打开 xcodeproj。按 ?B。CleanroomLogger.framework 从红色变为黑色。将框架拖到您的项目中。请参阅相关问题:Swift 3.1 的领域问题

build it

建造它