What kind of data can be a SwiftData property?
About 1 min
What kind of data can be a SwiftData property? 관련
SwiftData by Example
Back to Home
What kind of data can be a SwiftData property? | SwiftData by Example
What kind of data can be a SwiftData property?
Updated for Xcode 15
SwiftData is capable of storing a variety of data, but specifically the following:
- Any class that uses the
@Model
macro. - Any struct that conforms to the
Codable
protocol. - Any enum that conforms to the
Codable
protocol, even if it includes raw values or associated values.
Important
If you intend to use options 2 or 3 in a hybrid app – using both SwiftData and Core Data side by side – it’s important that you use composite attributes in your Core Data code in order to retain compatibility.
For example, we could create a Status
enum that has two cases, one of which has an associated value, then use that as one value inside
enum Status: Codable {
case active, inactive(reason: String)
}
@Model
class User {
var name: String
var status: Status
init(name: String, status: Status) {
self.name = name
self.status = status
}
}