Learn how to update Swift 3 and earlier code.
- [Instructor] You've probably had it happen to you. There's a project in Swift three, you wanna look at or update. However there's a few changes in Swift four that require an annoying conversion process and the documentation is terrible on how to do it. Let's look at how to convert Swift three projects easily to Swift four. First you get an old but not too old file. By old I mean in Swift three. Swift two and older you have to convert to Swift three using X code eight before this will work.
I'll use an old file of mine demonstrating navigation controllers which you can download from the GitHub repository. Open the project and you'll see two warnings on the project. Click on the warnings on top and you'll see the build warnings. The first is conversion to Swift four's available. If you click on that one, a window appears helping you convert it. Your target is selected so all you need to do is press next.
The next question is the big deal. Swift four removed objective C inference for efficiency reasons. The most common use of this is instances of UI button made completely in code. Programmatic targets for buttons and other controls use this inference. So there can be problems but ones you can fix easily by making all action targets IB actions. So go ahead and minimize inference and hit next. X code will tell you there's manual steps necessary.
There is a migration guide you can attempt to read but I don't suggest it. It doesn't tell you how to fix what's ahead and where to fix it. So just go ahead and hit okay. You'll get a message telling you this went successfully and it's time to do the updating to the project. Go ahead and update the project by clicking update. Once this finishes, you've got another warning. Click on the warning and you'll get the settings that will change. So you get some build settings and some enable recommended warnings.
And you can go ahead an perform those changes by hitting the perform changes button. This will do its thing and it looks like everything is working correctly. That's where you get into trouble. Click command B to build and you'll get one or two errors. These errors are build settings for the project and target relating to the objective C setting you clicked earlier. You have to manually turn them off. To do that, head back over to your build settings.
And in the search box, type atobjc. You'll see there is a setting for Swift objective C reference, and you wanna make it either off or default. Here it's fine. But it's showing a warning. You have to make sure you get it on all the targets. This is just on the project. You need it on the project and you need it on every target. So I selected the target and I could see here that it's still on in the target. So I'm gonna go down here and I can either turn it off or default.
I'm just gonna turn it to default. I'm gonna hit command B again and it goes away. That should clear the warning. You have a working project. Like most of my old projects, this project uses storyboards. And so there's no target action controls programmatically to give me errors. If you do have those, you may have to resolve those with an explicit at objective C, but better than that, use an IB action in front of the declaration of the action even for programmatic targets to prevent this error in the first place.
Now that should convert a Swift three project to the latest and greatest version.