在macOS的SwiftUI列表视图中选择和删除核心数据实体 [英] Select and Delete Core Data entities in SwiftUI List View on macOS

查看:107
本文介绍了在macOS的SwiftUI列表视图中选择和删除核心数据实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是SwiftUI的新手,但取得了合理的进步.我正在使用最新版本的Xcode 12.4,并运行BigSur 11.2.1.我正处于要使用核心数据的阶段,但是遇到了无法找到修复程序的问题.

I am a newbie to SwiftUI but making reasonable progress. I am using the latest version of Xcode 12.4 and running BigSur 11.2.1. I am at the stage where I want to use core-data but have run into an issue that I can't find a fix.

当我创建基本的Xcode项目时,我选择App和macOS作为模板然后我选择界面-SwiftUI,生命周期-SwiftUI应用,语言-迅捷然后选择使用核心数据

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

创建了一个新项目,并且可以在没有任何问题的情况下进行构建和运行.在出现的窗口中,只需单击顶部栏上的+按钮,即可添加新项(日期戳).到现在为止还挺好.这都是香草苹果代码.

A new project is created and Builds and Runs without any issues. In the window that appears I can add a new item (a datestamp) by simply clicking the + Button on the top bar. So far so good. This is all vanilla apple code.

我陷入困境的地方:-列表-ContentView中的ForEach视图不允许通过单击选择任何实体(项目),因此我找不到删除条目的方法.

Where I am stuck :- The List - ForEach View in the ContentView won't allow any of the Entities (items) to be selected by clicking and therefore I can't find a way to delete an entry.

如果我用一系列文本项替换实体,则可以选择它们并将其删除通过使用@State var selectKeeper = Set()选择:$ selectKeeper在列表视图中.

If I replace the Entities with an array of Text items then I can select them and delete them by using @State var selectKeeper = Set() with a selection: $selectKeeper in the List View.

有人可以解释如何做吗?

Can someone please explain how to do it?

这是内容视图的原始代码.

This is the vanilla code for the content view.

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中可能会为您提供所需的内容:

You should add EditButton() and probably wrap all of this in NavitagionView might give you what you are looking for:

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")
                }
            }
        }
    }
}

这篇关于在macOS的SwiftUI列表视图中选择和删除核心数据实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆