Swift Data
import Foundation
import SwiftData
@Model
class Destination {
var name: String
var details: String
var date: Date
var priority: Int
init(name: String = "", details: String = "", date: Date = .now, priority: Int = 2) {
self.name = name
self.details = details
self.date = date
self.priority = priority
}
}
import SwiftUI
import SwiftData
@main
struct iTourApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
.modelContainer(for: Destination.self)
}
}
@Environment(\.modelContext) var modelContext
func addSamples() {
let rome = Destination(name: "Rome")
let florence = Destination(name: "Florence")
let naples = Destination(name: "Naples")
modelContext.insert(rome)
modelContext.insert(florence)
modelContext.insert(naples)
}
func deleteDestinations(_ indexSet: IndexSet) {
for index in indexSet {
let destination = destinations[index]
modelContext.delete(destination)
}
}