I'm following a bootcamp course as I'm new to swift and coding and ran into the following issue. Any idea what is going on or how to fix it? I'm guessing it's something really simple but don't know a whole lot just yet. Thank you!
You've unfortunately ran into Swift 6's concurrency safety far earlier into your experience with Swift than you should have.
To solve this specific issue, you can add @MainActor
before getUserInfo
like this:
@MainActor func getUserInfo() {
...
}
You will probably run into more of these issues. My recommendation is to create a new "Command Line Tool" project (File -> New -> Project, then macOS -> Command Line Tool) and copy and paste the code into the main.swift
file in the command line tool project. The code should run without any issues in that project, and you can use the command line tool project for the remainder of your bootcamp.
I've found a better way! If you open up the right tab, you can adjust the "Playground Settings" and you can set it to "Swift 5", which will let you continue to use the playground.
"
Awesome, thank you for the help!
I'd like to provide more context to why you've got those errors, and why selecting Swift 5
version fixes the issue. The Swift compiler can run in several "modes", that is, sets of compilation checks that can be more or less restrictive. For example, there is a Swift 6 mode and a Swift 5 mode, and selecting Swift 5 version in the playground will use Swift 5 mode for compilation.
Swift 6 mode introduced, among other things, the concept of "strict concurrency checking", that is, a way for the compiler to tell the developer that the code as written might produce concurrency issues, that is, simultaneous access and mutation of variables from multiple functions running at the same time.
To do that, Swift 6 uses the concept of "actor isolation", that is, things can be "isolated" to a certain "actor": regardless of what "isolation" and "actor" mean, the compiler verifies that if 2 things are interacting, for example a function is referencing a variable, they must be "isolated" to the same "actor" (or have the same "isolation").
You are working in a Swift Playground, a very cool Swift feature that allows to write and execute Swift code interactively. It happens to be the case that in Swift Playgrounds global variables declared at "top level", for example username
and bio
in your code (by "top level" I mean "not embedded in a function or type"), are going to be "isolated to the main actor", which essentially means that they are going to be accessed from the main thread (the same that governs UI work in a UI application).
But functions, like getUserInfo
, are not "isolated to the main actor", so if you reference the username
and bio
variables from the getUserInfo
function you are going to have 2 things that interacts with each other while having "different isolation". Now, this doesn't necessarily create an issue in this particular example, but in more complex code it could, so the Swift compiler in Swift 6 mode is going to complain, while in Swift 5 mode this kind of check is disabled.
If you mark the getUserInfo
function with the @MainActor
attribute, you are making sure that the function is also "isolated to the main actor", so it can safely interact with the username
and bio
variables.
I agree that, for now, for an introductory course, the best option is to simply select Swift 5 mode, so these strict compiler checks are going to be disabled.
But the Swift community has also realized that, while these checks are useful and important, they should not bother developers that are learning the language through simple code examples, and actions are being taken to make sure that even in Swift 6 mode these errors are avoided. For example, as I mentioned, the username
and bio
variables are automatically "isolated to the main actor" in the Swift playground, but there's no reason why the getUserInfo
shouldn't be. If they both were isolated automatically, there would have been no compilation error.
Let us know if you need more help
Makes sense and really appreciate the explanation, thank you!