0

I'm an early bird in programming so I know this question can be ridiculous from the point of view of an expert but I'm stuck in this situation from several days. I would like to update a row by using a button "Edit" (pencil) after having used another button to store the item with a TextField.

Here's the code:

class Food: Hashable, Codable, Equatable {

    var id : UUID = UUID()
    var name : String

    init(name: String) {
        self.name = name
    }

    static func == (lhs: Food, rhs: Food) -> Bool {
        return lhs.name == rhs.name
    }

    func hash(into hasher: inout Hasher) {
    hasher.combine(name)
    }
}

class Manager: ObservableObject {

    let objectWillChange = PassthroughSubject<Void, Never>()

    @Published var shoppingChart: [Food] = []

    init() {

            let milk = Food(name: "Milk")
            let coffee = Food(name: "Coffee")

            shoppingChart.append(milk)
            shoppingChart.append(coffee)        
    }

    func newFood(name: String) {
        let food = Food(name: name)
        shoppingChart.insert(food, at: 0)
    }
}

struct ContentView: View {

    @ObservedObject var dm : Manager
    @State var isAddFoodOpened = false

    var body: some View {
        VStack {
            List {
                ForEach(self.dm.shoppingChart, id:\.self) { food in
                    HStack {
                    Text(food.name)
                    Image(systemName: "pencil")
                }
            }

        }
            self.buttonAdd
    }
}

    var buttonAdd: some View {
        Button(action: {
            self.isAddFoodOpened.toggle()
        }) {
            Text("Add")
        }
        .sheet(isPresented: $isAddFoodOpened) {
            Add(dm: self.dm, fieldtext: "", isAddFoodOpened: self.$isAddFoodOpened)
        }
    }
}

struct Add: View {

    @ObservedObject var dm : Manager
    @State var fieldtext : String = ""
    @Binding var isAddFoodOpened : Bool

    var body: some View {
        VStack {
        TextField("Write a food", text: $fieldtext)
        buttonSave
    }
    }

    var buttonSave : some View {
        Button(action: {
            self.dm.newFood(name: self.fieldtext)
            self.isAddFoodOpened = false
        }) {
            Text("Save")
        }
    }
}
Asperi
  • 228,894
  • 20
  • 464
  • 690
  • Just an unrelated note, if what makes a food object unique compared to other objects of Food class is the name, then make that the `id` property and avoid creating a uuid like: `var id: String { name }` or set `id` in your init. So, just return name when you need the id of a food. – Cenk Bilgen Apr 07 '20 at 14:38

2 Answers2

0

The @ObservedObject var dm : Manager object is never initialized.

Try initialized dm in ContentView like this:

@ObservedObject var dm = Manager()

0

Ok, so if I understand correctly you want to update/edit a row by using a button "Edit". This will do it:

struct ContentView: View {

@ObservedObject var dm : Manager
@State var isAddFoodOpened = false
@State var isEditOpened = false
@State var fieldtext : String = ""

var body: some View {
    VStack {
        List {
            ForEach(0..<self.dm.shoppingChart.count, id:\.self) { i in
                HStack {
                    Text(self.dm.shoppingChart[i].name)
                    Button(action: { self.isEditOpened.toggle() }) {
                        Image(systemName: "pencil")
                    }.sheet(isPresented: self.$isEditOpened) {
                        TextField(self.dm.shoppingChart[i].name, text: self.$fieldtext, onEditingChanged: { _ in
                            self.dm.shoppingChart[i].name = self.fieldtext
                        })
                    }
                }
            }
        }
        self.buttonAdd
    }
}

var buttonAdd: some View {
    Button(action: {
        self.isAddFoodOpened.toggle()
    }) {
        Text("Add")
    }
    .sheet(isPresented: $isAddFoodOpened) {
        Add(dm: self.dm, fieldtext: "", isAddFoodOpened: self.$isAddFoodOpened)
    }
}
}