Updated Xcode this morning and my SwiftData models are riddled with warnings about how this will be an error in Swift 6.
Cannot expand accessors on variable declared with 'let'; this is an error in the Swift 6 language mode
An example:
@Model
class Expense {
let id: UUID // warning here
var cashflow: Int
let isPermanent: Bool // warning here
var name: String
var asset: Asset?
var liability: Liability?
var player: Player?
var profession: Profession?
init(name: String, cashflow: Int, isPermanent: Bool) {
self.id = UUID()
self.cashflow = cashflow
self.isPermanent = isPermanent
self.name = name
}
}
I can make the warnings go away by changing them from let
to var
, but what do I do if I want those values to actually be unchangable? I don't want to be able to change the id
of the object after it is init'd, so declaring it as a var
feels really wrong.
You can use the private
access modifier to control who can write to the property
private(set) var id: UUID
var cashflow: Int
private(set) var isPermanent: Bool
var name: String
Then you only need to be careful with methods in the same class so you don't change the values there.