I've created a list of food products with stored data such as calories, protein, category, etc. I've made an array of categories in order to categorize these products into groups and I'm now trying to reference this array when stating the data in each product, but when doing this (Apple variable in line 16) I get the error:
"Cannot use instance member 'categories' within property initializer; property initializers run before 'self' is available"
This is my code:
struct Product : Identifiable {
var id = UUID()
var productName : String
var calories : Double
var protein : Double
var carbs : Double
var fats : Double
var weight : Double
var category : String
}
struct Data {
@State var categories = ["Animal Products", "Dairy", "Fast Food", "Fruits", "Grains", "Nuts", "Vegetables", "Wheat"]
var Apple = Product(productName: "Apple", calories: 52, protein: 0.3, carbs: 14, fats: 0.2, weight: 80, category: categories[3])
var Avocado = Product(productName: "Avocado", calories: 160, protein: 2, carbs: 9, fats: 15, weight: 600, category: <#T##String#>)
var Banana = Product(productName: "Banana", calories: 89, protein: 1.1, carbs: 23, fats: 0.3, weight: 120, category: <#T##String#>)
var Broccoli = Product(productName: "Broccoli", calories: 34, protein: 2.8, carbs: 5, fats: 0.4, weight: 225, category: <#T##String#>)
var Burger = Product(productName: "Burger", calories: 264, protein: 13, carbs: 30, fats: 10, weight: 110, category: <#T##String#>)
var Carrot = Product(productName: "Carrot", calories: 41, protein: 0.9, carbs: 10, fats: 0.2, weight: 61, category: <#T##String#>)
var Cheerios = Product(productName: "Cheerios", calories: 379, protein: 6, carbs: 83, fats: 5, weight: 40, category: <#T##String#>)
var Cherry = Product(productName: "Cherry", calories: 50, protein: 1, carbs: 12, fats: 0.3, weight: 5, category: <#T##String#>)
}
How can I include this array inside of the Product list?
lazy@State?