Question about using Arrays with the State property

Viewed 208

Currently, I am using @State for an Array of SKProducts

 @State var products = [SKProduct]()
    
    
    var body: some View {
        
        ScrollView(.horizontal){
            LazyHStack {
                if products.count > 0 {
                    ForEach(products.indices) { index in
                        let product = self.products[index]
                        
                        
                       // ProductCell(product: product)
                    }
                    ForEach(1...products.count, id: \.self) { count in
                        let product = $products[count]
                        
                       // ProductCell(product: Binding.constant(products[count]))
                    //    ProductCell(name: <#Binding<String>#>).padding()
                    }
                }
            }
        }.onAppear(perform: {
            Purchases.shared.offerings { (offerings, error) in
                if let tempPackages = offerings?.current?.availablePackages {
                    
                    for package in tempPackages {
                        products.append(package.product)
                    }
                }
            }
        })

        
    }

I am trying to pass off an individual product to another view below:

ForEach(1...products.count, id: \.self) { count in
                        let product = $products[count]
                    }

However "product" is considered an "error type"

enter image description here

I am new to SwiftUI and I cannot for the life of me figure out what I am doing wrong. Thank you for your time and consideration.

1 Answers

products is an array, $products is a binding.

products[0] is the first element of an array. $products[0] means nothing because a binding doesn't have a subscript accessor.

Remove the $.

Related