【问题标题】:Not append new data to an array不将新数据附加到数组
【发布时间】:2021-08-11 03:10:29
【问题描述】:

我想将新数据添加到项目中的列表中 但我做不到

我有一个用于显示产品列表的 ContentView.swift 视图 在另一个视图(ShopView)中,我想将数据添加到产品数组 我的产品数组和我的 addProduct() 函数 在 Products.swift 文件中

请帮帮我 谢谢

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")
    }
}

【问题讨论】:

    标签: swift xcode list swiftui swiftui-list


    【解决方案1】:

    现在,您正在创建Products 的两个不同实例。如果你想共享数据,你必须使用同一个实例

    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)) {  //<-- HERE
                        Text("Go Shop")
                    })
                .navigationBarTitle("Cart")
            }
        }
    }
    
    
    struct Shop: View {
        @ObservedObject var cart : Products //<-- HERE
        
        var body: some View {
            VStack{
                Button("Add Product To Cart") {
                    cart.addProduct(product: Product(name: "Name", price: 399))
                }
            }
        }
    }
    

    实现此类功能的另一种方法是使用环境对象。有关该方法的补充阅读:https://www.hackingwithswift.com/quick-start/swiftui/how-to-use-environmentobject-to-share-data-between-views

    【讨论】:

    • 非常感谢。
    【解决方案2】:

    我想你会发现最简单的实现是将你的模型定义为:

    class Products: ObservableObject {
        //Implement a shared resource here
        public static let shared = Products()
    
        // I would also make the published var name something different then the class name.
        // Otherwise you will be calling a lot of products.products.
        // Or you could change the name of the class to "Cart"
        @Published var products: [Product] = []
        
        func addProduct(product: Product) {
            products.append(product)
            print("Product Added")
        }
    }
    

    在你的结构中使用它: @ObservedObject var cart = Products.shared

    这将为您提供一个通用的共享产品类。您不必担心视图之间的传递。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-01
      • 2017-05-22
      • 2018-07-03
      • 1970-01-01
      • 1970-01-01
      • 2019-01-22
      • 2021-10-08
      • 1970-01-01
      相关资源
      最近更新 更多