I have the following var in an @Observable
class:
var displayResult: String {
if let currentResult = currentResult, let decimalResult = Decimal(string: currentResult) {
let result = decimalResult.formatForDisplay()
UIAccessibility.post(notification: .announcement, argument: "Current result \(result)")
return result
} else {
return "0"
}
}
The UIAccessiblity.post
gives me this warning:
Reference to static property 'announcement' is not concurrency-safe because it involves shared mutable state; this is an error in Swift 6
How can I avoid this?
You’re not doing anything unsafe here; this problem shows up because the announcement
property is a var
when, if you were writing this yourself, you would make it a let
.
The long-term fix would be for UIKit to make that change. In the short term, the workaround is to apply @preconcurrency
to your UIKit import. For example, this code doesn’t issue that warning:
@preconcurrency import UIKit
import Foundation
class MyModel: ObservableObject {
var displayResult: String {
let result = "test"
UIAccessibility.post(notification: .announcement, argument: "Current result \(result)")
return result
}
}
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"