I’m new to Swift and running into an issue with actor isolation in Xcode Playgrounds. When I try to run the following simple code, I get the error message about actor isolation, and I’m not sure why.
error: Testone.xcplaygroundpage:7:35: main actor-isolated var 'connectionSuccess' can not be referenced from a nonisolated context
if let newConnectionSuccess = connectionSuccess {
^
Testone.xcplaygroundpage:4:5: note: var declared here
var connectionSuccess: Bool? = nil
^
Testone.xcplaygroundpage:6:6: note: add '@MainActor' to make global function 'checkConnection()' part of global actor 'MainActor'
func checkConnection() -> Bool {
^
@MainActor
Code Swift v6.0 and Xcode v16
import Foundation
var connectionSuccess: Bool? = nil
func checkConnection() -> Bool {
if let newConnectionSuccess = connectionSuccess {
return newConnectionSuccess
}
return false
}
checkConnection()
I'm not working with threads or doing anything complicated, just trying to check the value of an optional Bool. The error suggests adding @MainActor, but I’ve seen similar code in a tutorial without this requirement. Is this something new in Swift? Appreciate your help with this.