How should I use a property of a type on main actor as default value of parameters?

@MainActor struct SomeView: UIViewRepresentable {
       func set(screenSize: CGSize = UIScreen.main.bounds.size) {...}
}

In case of that, I meet an error that says "class property 'main' isolated to global actor 'MainActor' can not be referenced from this synchronous context."
It also says the same error for 'bounds' .
Is there any way to fix this ? Xcode's version is 13.2.1

6 Likes

I would also like to get some guidance on this. For now I made an (ugly) workaround by also creating a global (non-class) constant.

(Explicitly declaring a static let as nonisolated does not work at the moment).

2 Likes

Anyone have any constructive solution for this? I have the issue while enabling aggressive compiler checks on XCode 14 beta 1 and tbh I have no idea how to solve it, except for extracting the values to some other global variable beforehand, which kinda kills the point.

As a practical workaround if the property in question can't be null I'd do this:

func set(screenSize: CGSize? = nil) {
    let screenSize = screenSize ?? UIScreen.main.bounds.size
    ...
}

Or this:

func set(screenSize: CGSize = CGSize(width: .nan, height: .nan)) {
    let screenSize = screenSize.width.isNaN ? UIScreen.main.bounds.size : screenSize
    ...
}