Locale Cannot Be Stored in SwiftData

Even though Locale is Codable, you'll encounter a fatal error.

If you have a SwiftData model that contains a Locale:

@Model
class Phone {
    var locale: Locale
}

Running this app will cause the following fatal error:

Thread 1: Fatal error: Unexpected property within Persisted Struct/Enum: _LocaleProtocol

This appears to be an Apple bug given that SwiftData supports any property that conforms to Codable (and Locale does).

As a workaround, you can store the Locale’s identifier and create a computed property to get and set it.

@Model
class Phone {
    typealias LocaleIdentifier = String
    private(set) var localeIdentifier: LocaleIdentifier
    var locale: Locale {
        get { Locale(identifier: localeIdentifier) }
        set { localeIdentifier = newValue.identifier }
    }
}