简体   繁体   中英

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. It was in 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).

I understand that I have to migrate my code to at least Swift 4, because Swift 3 is no longer supported.

The first thing I did was go to Edit... Convert... To Current Swift syntax. But I got the error "no target selected" and it would not let me select the target. I found advice online to change the Swift language version. I did this via Build Settings, where there is a dropdown, and I selected Swift 4.0 (it was previously 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."

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."

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!

Do I need to somehow get hold of an older version of XCode, or is there some other solution?

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.

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.

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 .

After I did this, the conversion worked to convert it from 4.0 to 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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