1

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.

  • 1
    You'll need to use an older version of XCode as an intermediate step. They are available for download from the Apple Developer site. – flanker Sep 27 '21 at 23:17
  • Luckily it turns out that's not the case! Phew (see my answer below). – Clare Sudbery Sep 27 '21 at 23:27
  • Sounds like you were lucky in that you weren't using any feature removed from the language. Or maybe 3-> 4 was non-breaking? Can't remember - it was a long time ago! Glad it's sorted anyway :) – flanker Sep 27 '21 at 23:44

1 Answers1

2

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.

  • 1
    That's a good answer, I suppose, but actually the whole exercise was a waste of time. You don't need automatic migration at all. It's nice, but it doesn't do anything a human brain can't do just as well, probably better in fact. You just set the project to Swift 5.0 and hit compile. If there are any issues, the compiler will tell you, and you just fix them. – matt Sep 27 '21 at 23:46
  • @matt That's fine if you don't have 100k+ lines of code in your project with 10k+ numbers of complaints from compiler. Lol. – Chus Sep 29 '21 at 07:27