[英]Select and Delete Core Data entities in SwiftUI List View on macOS
我是 SwiftUI 的新手,但取得了合理的進展。 我正在使用最新版本的 Xcode 12.4 並運行 BigSur 11.2.1。 我正處於想要使用核心數據的階段,但遇到了我找不到修復的問題。
When I create the basic Xcode project I select App and macOS as the template Then I select Interface - SwiftUI, Life Cycle - SwiftUI App, Language - Swift and select Use Core Data
一個新項目被創建並且構建和運行沒有任何問題。 在出現的 window 中,我只需單擊頂部欄上的 + 按鈕即可添加新項目(日期戳)。 到目前為止,一切都很好。 這都是香草蘋果代碼。
我被卡住的地方: - 列表 - ContentView 中的 ForEach 視圖不允許通過單擊選擇任何實體(項目),因此我找不到刪除條目的方法。
如果我用一組文本項替換實體,那么我可以 select 並使用 @State var selectKeeper = Set() 刪除它們,並在列表視圖中選擇:$selectKeeper。
有人可以解釋一下怎么做嗎?
這是內容視圖的原始代碼。
import SwiftUI
import CoreData
struct ContentView: View {
@Environment(\.managedObjectContext) private var viewContext
@FetchRequest(
sortDescriptors: [NSSortDescriptor(keyPath: \Item.timestamp, ascending: true)],
animation: .default)
private var items: FetchedResults<Item>
var body: some View {
List {
ForEach(items) { item in
Text("Item at \(item.timestamp!, formatter: itemFormatter)")
}
.onDelete(perform: deleteItems)
}
.toolbar {
Button(action: addItem) {
Label("Add Item", systemImage: "plus")
}
}
}
private func addItem() {
withAnimation {
let newItem = Item(context: viewContext)
newItem.timestamp = Date()
do {
try viewContext.save()
} catch {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
let nsError = error as NSError
fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
}
}
}
private func deleteItems(offsets: IndexSet) {
withAnimation {
offsets.map { items[$0] }.forEach(viewContext.delete)
do {
try viewContext.save()
} catch {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
let nsError = error as NSError
fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
}
}
}
}
private let itemFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateStyle = .short
formatter.timeStyle = .medium
return formatter
}()
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView().environment(\.managedObjectContext, PersistenceController.preview.container.viewContext)
}
}
您應該添加EditButton()並可能將所有這些都包裝在 NavitagionView 中,這可能會為您提供所需的內容:
var body: some View {
NavigationView{
List {
ForEach(items) { item in
Text("Item at \(item.timestamp!, formatter: itemFormatter)")
}
.onDelete(perform: deleteItems)
}
.toolbar {
ToolbarItem(placement: .navigationBarLeading) {
#if os(iOS)
EditButton()
#endif
}
ToolbarItem(placement: .navigationBarTrailing) {
Button(action: addItem) {
Label("Add Item", systemImage: "plus")
}
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.