- Swifty Snippets
- Posts
- Immutable SwiftData Model Properties
Immutable SwiftData Model Properties
How to workaround a Swift 6 language mode warning when using SwiftData
In Swift 5, we could declare a SwiftData Model with a let
property and not receive a warning.
@Model
class Person {
let yearOfBirth: Int
}
In Swift 6, we now receive the following warning.
Cannot expand accessors on variable declared with 'let'; this is an error in the Swift 6 language mode
Since we want to keep our codebase warning free whilst still minimising unnecessary public access, we can change the let
to a private(set) var
.
@Model
class Person {
private(set) var yearOfBirth: Int
}