Skip to content

Instantly share code, notes, and snippets.

@parpok
Created January 20, 2024 13:01
Show Gist options
  • Save parpok/a0b0c0b283de0a01f9eb63bfa251d822 to your computer and use it in GitHub Desktop.
Save parpok/a0b0c0b283de0a01f9eb63bfa251d822 to your computer and use it in GitHub Desktop.
import SwiftData
import SwiftUI
@available(iOS 17, *)
@Model
class TestData {
var id: UUID
var text: String
init(id: UUID, text: String) {
self.id = id
self.text = text
}
}
@available(iOS 17, *)
struct ContentView: View {
@Query var testData: [TestData]
@Environment(\.modelContext) private var modelContext
var body: some View {
Section {
List {
ForEach(testData, id: \.id) { data in
Text(data.text)
}.onDelete(perform: { indexSet in
for index in indexSet {
modelContext.delete(testData[index])
}
})
}
}
Section {
Button("ADD DATA") {
let dataExample: TestData = TestData(id: UUID(), text: "TEST")
modelContext.insert(dataExample)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment