[ad_1]
Please excuse me if I am utilizing the unsuitable nomenclature of issues, however that is the primary time I am utilizing CoreData.
I am enhancing textual content in one of many objects saved utilizing CoreData. The best way it really works on the app is when tapping on an merchandise in an inventory, a sheet pops up with the textual content that’s already there, and if modified, saves the brand new textual content.
I am scuffling with how one can cross the proper merchandise to the sheet, after which how one can replace the proper merchandise. How do you do that? I am undecided how passing information works.
This is what I labored out thus far:
struct ContentView: View {
@FetchRequest(entity: Jot.entity(), sortDescriptors: [NSSortDescriptor(keyPath: Jot.date, ascending: false)])
var jots: FetchedResults<Jot>
@State var selectedJot: Jot
@State var isNew = false
var physique: some View {
NavigationView {
Listing(jots) { jot in
Textual content(jot.textual content ?? "")
.onTapGesture() {
self.selectedJot = jot
isNew = false
}
}
.listStyle(.plain)
.toolbar {
ToolbarItem(placement: .bottomBar) {
Spacer()
}
ToolbarItem(placement: .bottomBar) {
Button(motion: {
isNew = true
self.sheetNewNote.toggle()
selectedItem = 0
}) {
Picture(systemName: "sq..and.pencil")
}
}
}
.sheet(merchandise: $selectedJot) { jot in
SheetView(isNewItem: $isNew, currJot: self.$selectedJot) // I get error Can not convert worth of kind 'Binding<Jot>' to anticipated argument kind 'Binding<Merchandise?>'
}
.sheet(isPresented: $sheetNewNote) {
SheetView(isNewItem: $isNew, currJot: self.$selectedJot) // no error right here
}
}
}
struct SheetView: View {
@Setting(.presentationMode) var presentationMode
@Binding var isNewItem: Bool
@Binding var currJot: Jot
@State var textual content: String = ""
var physique: some View {
NavigationView {
VStack {
TextEditor(textual content: $textual content)
.onAppear() {
if isNewItem {
textual content = currJot.textual content!
}
}
HStack {
Button("Cancel") {
presentationMode.wrappedValue.dismiss()
}
Spacer()
Button("Save") {
if isNewItem {
// create a brand new merchandise and reserve it with attempt viewContext.save()
} else {
// we're enhancing a present merchandise
updateJot(updatedJot: currJot)
attempt? viewContext.save()
}
presentationMode.wrappedValue.dismiss()
}
}
}
}
}
// Updates the merchandise
func updateJot(updatedJot: Jot){
if let jot = getById(id: updatedJot.objectID){
jot.textual content = updatedJot.textual content
}
}
// Will get a merchandise by id
func getById(id: NSManagedObjectID) -> Jot? {
return viewContext.object(with: id) as? Jot
}
}
}
EDIT:
attempting to grasp extra about how CoreData and SwiftUI work. Primarily based on the remark beneath, I ought to use @ObservableObject however after I went on the lookout for extra info, I could not discover any definitive examples.
This text additionally made much more complicated to me.
So, query nonetheless: how so I replace an merchandise already current through a sheet?
This, nevertheless, appears promising… I simply want to grasp what they did and see if I can replicate it on my code.
[ad_2]
