1

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?

4
  • First it is Swift naming convention to name your properties starting with a lowercase letter. Second Swift already have a native structure called Data. You should name it something else to avoid ambiguity Commented Oct 13, 2020 at 22:00
  • There is many ways to fix your issue. The easiest one is to declare your variables as lazy Commented Oct 13, 2020 at 22:04
  • Is it possible to change de categories? Commented Oct 13, 2020 at 23:21
  • Also, why do you need categories to be a @State? Commented Oct 14, 2020 at 2:17

1 Answer 1

1

It looks like categories are predefined, then here is a solution for your case - make categories static constant:

struct Data {
    static let 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])
   
    //  ... others
}
Sign up to request clarification or add additional context in comments.

2 Comments

Don't you need Self.categories[3]?
@pawello2222, no, compiler understands the scope, at least Xcode 12 / swift 5.3.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.