将nsmanageBjectId发送到结构 /视图

发布于 2025-02-02 01:46:31 字数 3679 浏览 1 评论 0原文

我是Swift,Swiftui和Coredata的新手。我在其他语言方面有良好的编程经验,但是Swift是它的世界。 :-)

重要信息:它是Macos而不是iOS!

我的问题:我想在表格中显示的单独视图中编辑数据集。我遵循此示例( swiftui更新视图在核心数据对象上>),但是当试图运行时,我的nsmanagebjectId是无所不能的。

ContentView(缩短)

import SwiftUI
import CoreData

struct ContentView: View {

    @State public var selectedBookId: NSManagedObjectID?

    @Environment(\.managedObjectContext) private var viewContext
    
    @FetchRequest(
        sortDescriptors: [NSSortDescriptor(keyPath: \Books.title, ascending: true)],
        animation: .default)
    private var books: FetchedResults<Books>
    @State private var showingEditScreen = false
    
    var body: some View {
        NavigationView {
            List {
                ForEach(books, id: \.self) { book in
                    HStack {
                        NavigationLink {
                            HStack {
                                Button {
                // here store objectID to var
                                    selectedBookId = book.objectID
                                    showingEditScreen.toggle()
                                } label: {
                                    Label("", systemImage: "pencil")
                                }
                            }
                            .padding(10.0)
                        } label: {
                            Text(book.title!)
                        }
                    }
                }.onDelete(perform: deleteBooks)
            }
            .toolbar {
                ToolbarItem(placement: .automatic) {
                    // here goes blabla
                }
            }
            Text("Bitte zuerst ein Buch auswählen!")
        }
        .sheet(isPresented: $showingEditScreen) {
    // Run EditBookView an send bookId
            EditBookView(bookId: selectedBookId).environment(\.managedObjectContext, self.viewContext)
        }
    }
}

我的editview看起来像这样

import SwiftUI

struct EditBookView: View {
    @Environment(\.managedObjectContext) var moc
    @Environment(\.dismiss) var dismiss
    var bookId: NSManagedObjectID!  // This is allways nil!!
    var book: Books {
        moc.object(with: bookId) as! Books
    }
    
    @State private var title = ""
    @State private var review = ""
    
    var body: some View {
            Form {
                Text("Edit Book").font(.title)
                Spacer()
                Section {
                    TextField("Buchname", text: $title)
                    TextEditor(text: $review)
                } header: {
                    Text("Schreibe eine Zusammenfassung")
                }
                Spacer()
                Section {
                    HStack {
                        Button("Save") {
                            // add the book
                            // here code for update 
                            try? moc.save()
                            dismiss()
                        }
                        Button("Cancel") {
                            print(bookId) // shows "nil"
                            dismiss()
                        }
                    }
                }
                Spacer()
            }
        .onAppear {
            self.title = self.book.title ?? ""
            self.review = self.book.review ?? ""
        }
        .padding(10.0)
        
    }
}

I'm complete new to swift, swiftui and coredata. I have good programming experience in other languages, but swift is its own world. :-)

Important information: it's for macOS not iOS!!

My problem: I want to edit a Dataset in an separate view displayed in a sheet. I followed this example (SwiftUI update view on core data object change), but when trying to run, my NSManagedObjectID is allway nil.

The ContentView (shortened)

import SwiftUI
import CoreData

struct ContentView: View {

    @State public var selectedBookId: NSManagedObjectID?

    @Environment(\.managedObjectContext) private var viewContext
    
    @FetchRequest(
        sortDescriptors: [NSSortDescriptor(keyPath: \Books.title, ascending: true)],
        animation: .default)
    private var books: FetchedResults<Books>
    @State private var showingEditScreen = false
    
    var body: some View {
        NavigationView {
            List {
                ForEach(books, id: \.self) { book in
                    HStack {
                        NavigationLink {
                            HStack {
                                Button {
                // here store objectID to var
                                    selectedBookId = book.objectID
                                    showingEditScreen.toggle()
                                } label: {
                                    Label("", systemImage: "pencil")
                                }
                            }
                            .padding(10.0)
                        } label: {
                            Text(book.title!)
                        }
                    }
                }.onDelete(perform: deleteBooks)
            }
            .toolbar {
                ToolbarItem(placement: .automatic) {
                    // here goes blabla
                }
            }
            Text("Bitte zuerst ein Buch auswählen!")
        }
        .sheet(isPresented: $showingEditScreen) {
    // Run EditBookView an send bookId
            EditBookView(bookId: selectedBookId).environment(\.managedObjectContext, self.viewContext)
        }
    }
}

My EditView looks like this

import SwiftUI

struct EditBookView: View {
    @Environment(\.managedObjectContext) var moc
    @Environment(\.dismiss) var dismiss
    var bookId: NSManagedObjectID!  // This is allways nil!!
    var book: Books {
        moc.object(with: bookId) as! Books
    }
    
    @State private var title = ""
    @State private var review = ""
    
    var body: some View {
            Form {
                Text("Edit Book").font(.title)
                Spacer()
                Section {
                    TextField("Buchname", text: $title)
                    TextEditor(text: $review)
                } header: {
                    Text("Schreibe eine Zusammenfassung")
                }
                Spacer()
                Section {
                    HStack {
                        Button("Save") {
                            // add the book
                            // here code for update 
                            try? moc.save()
                            dismiss()
                        }
                        Button("Cancel") {
                            print(bookId) // shows "nil"
                            dismiss()
                        }
                    }
                }
                Spacer()
            }
        .onAppear {
            self.title = self.book.title ?? ""
            self.review = self.book.review ?? ""
        }
        .padding(10.0)
        
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尘世孤行 2025-02-09 01:46:31

第一:感谢所有的好提示。最后,我可以使用

@ObservedObject var aBook: Books

在我的EditView的开头。

该按钮本身具有以下代码

Button {
    showingEditScreen.toggle()
} label: {
    Label("", systemImage: "pencil")
}.sheet(isPresented: $showingEditScreen) {
    EditBookView(aBook: book).environment(\.managedObjectContext, self.viewContext)
}

这样,我可以将单书项目的整本书对象发送到编辑视图中,我可以使用它。

First: thanks for all the good hints. In the end, I could solve the problem using

@ObservedObject var aBook: Books

at the beginning of my EditView.

The button itself has the following code

Button {
    showingEditScreen.toggle()
} label: {
    Label("", systemImage: "pencil")
}.sheet(isPresented: $showingEditScreen) {
    EditBookView(aBook: book).environment(\.managedObjectContext, self.viewContext)
}

This way, I can send the whole book object of a single book item to the edit view and I can use it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文