2024-12-17
用@Persisted关键字确定项目需要保存。
import RealmSwift
class Item: Object, Identifiable{
@Persisted var title: String = ""
@Persisted var done: Bool = false
}
用@ObservedResults()关键字确定项目是动态修改的。
@ObservedResults(Item.self) var items
用List来展示数据
List{
ForEach(items, id: \.selft) { item in
Text(item.title)
}
}
通过realm.write
和realm.add
实现
NavigationView{}
.navigationBarItems(
trailing: Button("添加"){
let realm = try! Realm()
try! reaml.write{
let newItem = Item()
newItem.title = title
newItem.done = false
realm.add(newItem)
}
}
)
完整代码参考:
import SwiftUI
import RealmSwift
struct AddItemView: View {
@Binding var isPresented: Bool
@State private var title = ""
var body: some View {
NavigationView {
Form {
TextField("项目名称", text: $title)
}
.navigationTitle("添加新项目")
.navigationBarItems(
leading: Button("取消") {
isPresented = false
},
trailing: Button("添加") {
let realm = try! Realm()
try! realm.write {
let newItem = Item()
newItem.title = title
newItem.done = false
realm.add(newItem)
}
isPresented = false
}
.disabled(title.isEmpty)
)
}
}
}
比如修改.done属性,需要解冻项目.thaw()
List {
ForEach(items, id: \.self) { item in
Toggle(item.title, isOn: Binding(
get: { item.done },
set: { newValue in
if let thawedItem = item.thaw() {
try? thawedItem.realm?.write {
thawedItem.done = newValue
}
}
}
))
}
也需要解冻项目.thaw()
Button(role: .destructive) {
if let thawedItem = item.thaw() {
try? thawedItem.realm?.write {
thawedItem.realm?.delete(thawedItem)
}
}
} label: {
Label("删除", systemImage: "trash")
}