UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Edit an item fetched from CoreData into swiftUI List

Forums > SwiftUI

I wrote this code, it adds new item and delete it perfectly, but I can not make an edit on any item. I copied the item into Textfield in a sheet so I can modify it. I tried to make i.toDo = toEdit, and save it but I think it is not the correct way.I searched about the problem but no way to the answer.

import SwiftUI
import CoreData
struct ContentView: View {
var context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
@State var showEditSheet = false
@State var toEdit = ""
@FetchRequest(
    sortDescriptors: [NSSortDescriptor(keyPath: \Note.toDo, ascending: true)], animation: .default)
var items : FetchedResults<Note>
@State private var task = ""
var body: some View {

    NavigationView{
        VStack(){
            List{
                ForEach(items) { i in

                Text(i.toDo!)
                    .onTapGesture{
                    showEditSheet.toggle()
                        toEdit = i.toDo!
                    }
                    .sheet(isPresented: $showEditSheet){
                        VStack{
                        TextField("",text: $toEdit)
                            Button(action:{

                                 /// Editing here ///
                                /// It does not work///

                                try! context.save()

                            }
                                   ){
                                Text("save")
                            }
                    }
                    }
                }.onDelete(perform: remove)
            }.navigationBarTitle(Text("Note"))

            TextField("note here ...", text: $task)
            Button(action:{
                let newNote = Note(context: context)
                newNote.toDo = task

                do{
                    try context.save()
                }catch{print(error.localizedDescription)}
            }){
                Text("save")
            }
        }.padding()
    }
}

func remove(offsets : IndexSet){
    offsets.map{
        items[$0]}.forEach(context.delete)

    do{
        try context.save()
    }catch{}
    }

3      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.