We are migrating to swift 6 from swift 5 using Xcode 16.2. we are getting below errors in almost each of our source code files :
Call to main actor-isolated initializer 'init(storyboard:bundle:)' in a synchronous non isolated context Main actor-isolated property 'delegate' can not be mutated from a nonisolated context Call to main actor-isolated instance method 'register(cell:)' in a synchronous nonisolated context Call to main actor-isolated instance method 'setup()' in a synchronous nonisolated context
Few questions related to these compile errors.
Some of our functions arguments have default value set but swift 6 does not allow to set any default values. This requires a lot of code changes throughout the project. This would be lot of source code re-write. Using annotations like @uncheck sendable , @Sendable on the class (Main actor) name, lot of functions within those classes , having inside some code which coming from other classes which also showing main thread issue even we using @uncheck sendable.
There are so many compile errors, we are still seeing other than what we have listed here. Fixing these compile errors throughout our project, would be like a re-write of our whole application, which would take lot of time. In order for us to migrate efficiently, we have few questions where we need your help with. Below are the questions.
Are there any ways we can bypass these errors using any keywords or any other way possible? Can Swift 5 and Swift 6 co-exist? so, we can slowly migrate over a period of time.
Migrating a large codebase to Swift 6 is not a trivial task. You can expect to encounter a significant number of issues. However, it’s possible to make rapid progress because, in general, these issues falls into big groups. Once you work on the solution for one of the issues in a group you can apply the same solution to all other issues in that group.
There are a couple of standard resources that I generally point folks at here:
-
WWDC 2024 Session 10169 Migrate your app to Swift 6
Additionally, the Swift team is actively exploring ways to make this process easier. For a general explanation of the plan, see the Improving the approachability of data-race safety vision doc. There are are a bunch of in-flight Swift Evolution proposals that work towards that vision.
So, you have a choice here:
-
You can choose to tackle this today.
-
Or you can wait for those changes to land in a production Swift compiler.
If you want to tackle this today then I’m happy to help, but I need to see concrete examples. If you decide to go down this path, see Quinn’s Top Ten DevForums Tips for general advice on how to craft a question that’s easy to answer.
Coming back to your current questions:
Are there any ways we can bypass these errors using any keywords or any other way possible?
That very much depends on the context. For UI code, which seems to be where you’re hitting the most problems, my general advice is:
-
Put everything on to the main actor by default.
-
Only move code off the main actor when there’s a specific reason to do so, for example, when you’re doing some CPU intensive work and need the UI to remain responsible in the meantime.
Notably, this general approach is one of the key ideas of the above-mentioned vision document, it’s just the the upcoming Swift compiler will make it easier to implement it.
Can Swift 5 and Swift 6 co-exist?
Yes. The ideal way to do this is on a module-by-module basis, where each module gets to choose the language mode it uses. However, even within the same module it’s generally possible to write code that compiles in both the Swift 5 and 6 language modes. Most of the time that just works. I’m happy to help out in situations where it doesn’t but, again, I need to see a concrete problems.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"