英文:
Update data in SwiftData
问题
I am trying to update data in SwiftData. The documentation, however, is literally just:
func update(expressions: [String : NSExpression],
model: any PersistentModel.Type,
where predicate: NSPredicate? = nil) throws -> Bool
As a new developer, I have no clue what most of this means or how to use it. My code is below:
try? context.update(expressions ["teamScores":NSExpression(format: "\(teamScores)")], model: CounterModel.self)
It failed with exception of type NSException
.
I haven't been able to try much, honestly. With such a new framework having literally been released 6 days ago as of writing this, there isn't much documentation or examples outside of setting up the model, persisting data, and querying the database.
英文:
I am trying to update data in SwiftData. The documentation, however, is literally just:
func update(expressions: [String : NSExpression],
model: any PersistentModel.Type,
where predicate: NSPredicate? = nil) throws -> Bool
As a new developer, I have no clue what most of this means or how to use it. My code is below:
try? context.update(expressions ["teamScores":NSExpression(format: "\(teamScores)")], model: CounterModel.self)
It failed with exception of type NSException
.
I haven't been able to try much, honestly. With such a new framework having literally been released 6 days ago as of writing this, there isn't much documentation or examples outside of setting up the model, persisting data, and querying the database.
答案1
得分: 1
To update the data you can bind directly to an object via @Bindable
and change the values you want. Changes should then be saved/persisted automatically.
Check out the Build an app with SwiftData session (at 4:11). The code basically boils down to:
@Model
final class Item {
var text: String
}
struct EditorView: View {
@Bindable var item: Item
var body: some View {
TextField("Item Text", text: $item.text)
}
}
PS: In this example the Item is fetched via a @Query
in the main view and passed on this EditorView.
英文:
To update the data you can bind directly to an object via @Bindable
and change the values you want. Changes should then be saved/persisted automatically.
Check out the Build an app with SwiftData session (at 4:11). The code basically boils down to:
@Model
final class Item {
var text: String
}
struct EditorView: View {
@Bindable var item: Item
var body: some View {
TextField("Item Text", text: $item.text)
}
}
PS: In this example the Item is fetched via a @Query
in the main view and passed on this EditorView.
答案2
得分: 0
以下是翻译好的内容:
要在SwiftData中更新数据,我们不需要直接操作modelContext。我们只需要更新实例,数据将自动更新。
这是一个示例,其中包含一个简单的添加列表项目和在点击时跟踪列表的示例:
@Model final class AnyItem {
var name: String
var didTapped: Bool = false
init(name: String) {
self.name = name
}
}
struct ContentView: View {
@State var itemName: String = ""
@Environment(\.modelContext) private var modelContext
@Query var items: [AnyItem]
var body: some View {
NavigationStack {
List {
Section {
TextField("Item name", text: $itemName)
Button("Submit", action: addItem)
}
ForEach(items) { item in
Section {
Text(item.name)
Text("Did tapped: \(item.didTapped.description)")
}
.onTapGesture {
updateItem(item)
}
}
}
}
}
private func addItem() {
withAnimation {
let newItem = AnyItem(name: itemName)
modelContext.insert(newItem)
itemName = ""
}
}
private func updateItem(_ item: AnyItem) {
withAnimation {
item.didTapped = true
}
}
}
#Preview {
ContentView()
.modelContainer(for: AnyItem.self, inMemory: true)
}
请注意,已将HTML实体编码(如"
)还原为原始文本。
英文:
To update data in SwiftData, we don't need to touch the modelContext directly. We just need to update the instance and the data would be updated automatically.
Here's an example with simple add list project and keep list tracked when didTapped:
@Model final class AnyItem {
var name: String
var didTapped: Bool = false
init(name: String) {
self.name = name
}
}
struct ContentView: View {
@State var itemName: String = ""
@Environment(\.modelContext) private var modelContext
@Query var items: [AnyItem]
var body: some View {
NavigationStack {
List {
Section {
TextField("Item name", text: $itemName)
Button("Submit", action: addItem)
}
ForEach(items) { item in
Section {
Text(item.name)
Text("Did tapped: \(item.didTapped.description)")
}
.onTapGesture {
updateItem(item)
}
}
}
}
}
private func addItem() {
withAnimation {
let newItem = AnyItem(name: itemName)
modelContext.insert(newItem)
itemName = ""
}
}
private func updateItem(_ item: AnyItem) {
withAnimation {
item.didTapped = true
}
}
}
#Preview {
ContentView()
.modelContainer(for: AnyItem.self, inMemory: true)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论