Created
January 20, 2024 13:01
-
-
Save parpok/a0b0c0b283de0a01f9eb63bfa251d822 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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