将 NSManagedObjectID 发送到结构/视图
Sending an NSManagedObjectID to a struct / view
我是 swift、swiftui 和 coredata 的新手。我有很好的其他语言编程经验,但 swift 是它自己的世界。 :-)
重要信息:它适用于 macOS 而不是 iOS!!
我的问题:我想在 sheet 中显示的单独视图中编辑数据集。我遵循了这个例子 (),但是当尝试 运行 时,我的 NSManagedObjectID 总是 nil。
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)
}
}
首先:感谢所有好的提示。最后,我可以使用
解决问题
@ObservedObject var aBook: Books
在我的 EditView 的开头。
按钮本身有如下代码
Button {
showingEditScreen.toggle()
} label: {
Label("", systemImage: "pencil")
}.sheet(isPresented: $showingEditScreen) {
EditBookView(aBook: book).environment(\.managedObjectContext, self.viewContext)
}
这样,我可以将单个图书项目的整个图书对象发送到编辑视图,然后我就可以使用它了。
我是 swift、swiftui 和 coredata 的新手。我有很好的其他语言编程经验,但 swift 是它自己的世界。 :-)
重要信息:它适用于 macOS 而不是 iOS!!
我的问题:我想在 sheet 中显示的单独视图中编辑数据集。我遵循了这个例子 (
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)
}
}
首先:感谢所有好的提示。最后,我可以使用
解决问题@ObservedObject var aBook: Books
在我的 EditView 的开头。
按钮本身有如下代码
Button {
showingEditScreen.toggle()
} label: {
Label("", systemImage: "pencil")
}.sheet(isPresented: $showingEditScreen) {
EditBookView(aBook: book).environment(\.managedObjectContext, self.viewContext)
}
这样,我可以将单个图书项目的整个图书对象发送到编辑视图,然后我就可以使用它了。