Compile errors - function arguments not on MainActor

I'm having trouble porting some code to the latest Swift / SwiftUI. The code below creats a Binding. I now get errors like:

Property 'currentColoring' isolated to global actor 'MainActor' can not be referenced from this synchronous context. Add '@MainActor' to make local function 'set' part of global actor 'MainActor'

But if I do that, then the call to Binding.init won't compile:

Converting function value of type '@MainActor () -> HueSat' to '() -> HueSat' loses global actor 'MainActor'

Is there a way out of this?

As far as I can tell, all the code is on the MainActor. I'm just having trouble telling the compiler that.

func hueSatBinding() -> Binding<HueSat> {
    func get() -> HueSat {
        return hueSat
    }
    func set(_ new: HueSat) {
        hueSat = new
        guard let uiColor = model.currentColoring as? UIColor else { return }
        let (_,_,v,a) = uiColor.getHSVA()
        model.currentColoring = UIColor(hue: new.h, saturation: new.s, brightness: v, alpha: a)
    }
    return Binding(get: get, set: set)
}

Well, it works if I pass anonymous closures:

Binding { ... } set: { new in ... }

Not sure why, but I'll take it.

Does it work if you mark your get explicitly @MainActor?

No.

Terms of Service

Privacy Policy

Cookie Policy