Xcode — SwiftData

Preserving #

@Model

Lets SwiftData save instances of a model class

import SwiftData

// Annotate new or existing model classes with the @Model macro.
@Model
class Trip {
    var name: String
    var destination: String
    var startDate: Date
    var endDate: Date
    var accommodation: Accommodation?
}

@Attribute

@Attribute(.unique) var name: String

@Relationship

@Relationship(.cascade) var accommodation: Accommodation?

@Transient

temporary data that doesn’t need saving.

@Transient var destinationWeather = Weather.current()

modelContainer: set up the default storage with options.

ModelConfiguration

ModelContext

import SwiftUI
import SwiftData

struct ContentView: View {
    @Environment(\.modelContext) private var context
}

@Query

Syncing #

Building #

Adopting #

本文共 96 字,上次修改于 Oct 24, 2024
相关标签: Xcode, Swift, ByAI