Can I create a getter/setter capable of mutating a value inside of an Actor

15 hours ago 4
ARTICLE AD BOX

I'm trying to bring a Veteran NSObject to the UI with as much modern concurrency saftey as possible.

I could change the actor to a class and all of my errors go away. For saftey I'm trying to keep it.

VeteranNSObject - A non-Sendable object with about 6 identical streams inside. Wish they were enum'd.

Key - A Dev Defined "map" of values and functions inside of VeteranNSObject. 1 static var for each stream.

UICall - The only calls that will be publically usable.

GOAL: A getter & setter capable of mutating a value inside of the Actor.

Example:

struct ContentView: View { let calls = UICall() var body: some View { Circle() .task { async let x = calls.mutableValue // gets value await calls.mutableValue = 3.13 // sets value } } }

I already figured out that I can add getMutableValue() & setMutableValue(to:_) to UICalls.

Its really just bothering me that I cannot figure out how to create a getter/setter that maps to an Actor...

Does anyone know a pattern that might help?

Or please explain to me why it's impossible?

nonisolated // `@preconcurrency import` candidate. NonSendable open class VeteranNSObject: NSObject { var mutableValue: Double = 1.0 } fileprivate actor MyActor { static let shared = MyActor() var veteranNSObject = VeteranNSObject() func setMutableValue ( key: Key , to: Double ) { key.setMutableValue ( veteranNSObject , to ) } func getMutableValue ( key: Key ) -> Double { key.getMutableValue ( veteranNSObject ) } } fileprivate struct Key : Sendable { let getMutableValue : @Sendable ( VeteranNSObject ) -> Double let setMutableValue : @Sendable ( VeteranNSObject , Double ) -> Void static let devDefinedKey = Key ( getMutableValue: { $0.mutableValue } , setMutableValue: { $0.mutableValue = $1 } ) } public struct UICalls { private let key: Key = .devDefinedKey // public var getMutableValue: Double { get async { await MyActor.shared.getMutableValue ( key: key ) } } // public func setMutableValue ( to: Double ) async { await MyActor.shared.setMutableValue ( key: key , to: to ) } var mutableValue: Double { get async { await MyActor.shared.getMutableValue ( key: key ) } set { MyActor.shared.setMutableValue ( key: key , to: newValue ) } } }
Read Entire Article