简体   繁体   English

如何使用 XCode 12.4 从 Swift 3 迁移?

[英]How do I migrate from Swift 3 using XCode 12.4?

I've searched for similar problems but I can't find anyone describing anything quite like this...我已经搜索过类似的问题,但我找不到任何人描述这样的事情......

I just opened a Swift project after having not looked at it for three years. Swift项目三年没看,刚打开。 It was in Swift 3.它在 Swift 3。

When I opened XCode it automatically installed some updates and is now XCode 12.4 (I don't know what it was before).当我打开 XCode 时,它自动安装了一些更新,现在是 XCode 12.4(我不知道之前是什么)。

I understand that I have to migrate my code to at least Swift 4, because Swift 3 is no longer supported.我知道我必须至少将我的代码迁移到 Swift 4,因为不再支持 Swift 3。

The first thing I did was go to Edit... Convert... To Current Swift syntax.我做的第一件事是 go 编辑...转换...到当前 Swift 语法。 But I got the error "no target selected" and it would not let me select the target.但是我得到了“没有选择目标”的错误,它不会让我 select 成为目标。 I found advice online to change the Swift language version.我在网上找到建议更改Swift语言版本。 I did this via Build Settings, where there is a dropdown, and I selected Swift 4.0 (it was previously 3.0).我通过 Build Settings 执行此操作,那里有一个下拉菜单,我选择了 Swift 4.0(以前是 3.0)。 This allowed me to select a target for conversion, but then I got the error saying the convert failed, and "Please ensure that all selected targets build successfully with the currently configured Swift version before attempting a migration."这允许我将 select 作为转换目标,但随后我收到错误消息,指出转换失败,并且“请确保所有选定的目标在尝试迁移之前使用当前配置的 Swift 版本成功构建。”

The advice I've seen says that I should first make sure I can build the code before I attempt to migrate.我看到的建议是,我应该首先确保在尝试迁移之前可以构建代码。 But I can't build it because I get errors saying "SWIFT_VERSION '3.0' is unsupported, supported versions are: 4.0, 4.2, 5.0."但我无法构建它,因为我收到错误消息“SWIFT_VERSION '3.0' 不受支持,支持的版本为:4.0、4.2、5.0。”

I seem to be stuck between a rock and a hard place: I can't convert the code to Swift 4 because it won't build, but I can't build it because it's not Swift 4!我似乎进退两难:我无法将代码转换为 Swift 4,因为它无法构建,但我无法构建它,因为它不是 Swift 4!

Do I need to somehow get hold of an older version of XCode, or is there some other solution?我需要以某种方式获取 XCode 的旧版本,还是有其他解决方案?

Incidentally I can't now change the Swift language version in Build Settings back to 3.0, because that option is not in the dropdown.顺便说一下,我现在无法将构建设置中的 Swift 语言版本改回 3.0,因为该选项不在下拉列表中。

Aha, I found a solution.啊哈,我找到了解决办法。

I found it via this question .我通过这个问题找到了它。

It turns out I needed to go into MyProjectName.xcodeproj/project.pbxproj and change all references to SWIFT_VERSION from 3.0 to 4.0.事实证明,我需要将 go 放入MyProjectName.xcodeproj/project.pbxproj并将对SWIFT_VERSION的所有引用从 3.0 更改为 4.0。

I did this via vim MyProjectName.xcodeproj/project.pbxproj and then in Vim I used :%s/SWIFT_VERSION = 3.0/SWIFT_VERSION = 4.0/g .我通过vim MyProjectName.xcodeproj/project.pbxproj做到了这一点,然后在 Vim 中我使用了:%s/SWIFT_VERSION = 3.0/SWIFT_VERSION = 4.0/g

After I did this, the conversion worked to convert it from 4.0 to 5.0.在我这样做之后,转换工作将它从 4.0 转换为 5.0。 In fact it seemed almost no changes were needed apart from a very small syntax change in AppDelegate.swift , and this time the tool itself made the equivalent change I'd made previously - ie changed SWIFT_VERSION from 4.0 to 5.0 in project.pbxproj .事实上,除了AppDelegate.swift中非常小的语法更改外,似乎几乎不需要任何更改,这次工具本身进行了我之前所做的等效更改 - 即在project.pbxproj SWIFT_VERSION从 4.0 更改为 5.0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM