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
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
struct Product: Identifiable {
var id = UUID()
var name: String
var price: Int
}
Shop.swift
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
class Products: ObservableObject {
@Published var products = [Product]()
func addProduct(product: Product) {
products.append(product)
print("Product Added")
}
}
2
Answers
Right now, you’re creating two different instances of
Products
. If you want the data to be shared, you have to use the same instance.Another way to achieve this type of functionality is by using an environment object. Additional reading on that approach: https://www.hackingwithswift.com/quick-start/swiftui/how-to-use-environmentobject-to-share-data-between-views
I think you will find the simplest implementation will be to define your model as:
Use it in your structs with:
@ObservedObject var cart = Products.shared
This will give you one common shared Products class. You don’t have to worry about passing is around between views.