Not append new data in an array

Hi
I want to add new data to a list in my project
but I can't do it

I have a ContentView.swift view for showing products list
and in another view (ShopView) I want to add data to products array
My products array and my addProduct() function
in the Products.swift file

Please help me
Thanks

ContentView.swift
Code Block
struct ContentView: View {
    @ObservedObject var cart = Products()
    var body: some View {
        NavigationView{
            List {
                ForEach(cart.products) { product in
                    Text("\(product.name) \(product.price)$")
                }
            }
            .navigationBarItems(
                trailing: NavigationLink(destination: Shop()) {
                    Text("Go Shop")
                })
            .navigationBarTitle("Cart")
        }
    }
}

Product.swift
Code Block
struct Product: Identifiable {
    var id = UUID()
    var name: String
    var price: Int
}

Products.swift
Code Block
struct Shop: View {
    @ObservedObject var cart = Products()
    var body: some View {
        VStack{
            Button("Add Product To Cart") {
                cart.addProduct(product: Product(name: "Name", price: 399))
            }
        }
    }
}

Products.swift
Code Block
class Products: ObservableObject {
    @Published var products = [Product]()
    func addProduct(product: Product) {
        products.append(product)
        print("Product Added")
    }
}






Answered by workingdogintokyo in 675305022
what you are doing is resetting the "cart" to empty each time you call "Shop". Note, passing variables and etc... to another View is an important concept to master.
Try this:


Code Block
struct ContentView: View {
@ObservedObject var cart = Products()
var body: some View {
NavigationView {
List {
ForEach(cart.products) { product in
Text("\(product.name) \(product.price)$")
}
}
.navigationBarItems(
trailing: NavigationLink(destination: Shop(cart: cart)) { // <----
Text("Go Shop")
})
.navigationBarTitle("Cart")
}.navigationViewStyle(StackNavigationViewStyle())
}
}
struct Shop: View {
@ObservedObject var cart:Products // <----
var body: some View {
VStack{
Button("Add Product To Cart") {
cart.addProduct(product: Product(name: "Name", price: 399))
}
}
}
}


Accepted Answer
what you are doing is resetting the "cart" to empty each time you call "Shop". Note, passing variables and etc... to another View is an important concept to master.
Try this:


Code Block
struct ContentView: View {
@ObservedObject var cart = Products()
var body: some View {
NavigationView {
List {
ForEach(cart.products) { product in
Text("\(product.name) \(product.price)$")
}
}
.navigationBarItems(
trailing: NavigationLink(destination: Shop(cart: cart)) { // <----
Text("Go Shop")
})
.navigationBarTitle("Cart")
}.navigationViewStyle(StackNavigationViewStyle())
}
}
struct Shop: View {
@ObservedObject var cart:Products // <----
var body: some View {
VStack{
Button("Add Product To Cart") {
cart.addProduct(product: Product(name: "Name", price: 399))
}
}
}
}


Not append new data in an array
 
 
Q