Okay thanks for your tip, I didn’t know this at frist.
Here is my code:
//
// recipe.swift
// recipe list app
//
// Created by Rune Pollet on 25/01/2022.
//
**import** Foundation
**struct** Recipe: Identifiable {
**var** id:String
**var** name:String
**var** featured:Bool
**var** image:String
**var** description:String
**var** prepTime:String
**var** cookTime:String
**var** totalTime:String
**var** servings:Int
**var** highlights:[String]
**var** ingredients:[Ingredient]
**var** directions:[String]
}
**struct** Ingredient: Identifiable {
**var** id:String
**var** name:String
**var** num:Int?
**var** denom:Int?
**var** unit:String?
}
func getData() {
// Properties for the database and the recipes collection
let db = Firestore.firestore()
let recipes = db.collection("recipes")
// Get all the documents in the recipes collection
recipes.getDocuments { querySnapshot, error in
if error == nil && querySnapshot != nil {
// Property for the array of books
var recipes = [Recipe]()
for doc in querySnapshot!.documents {
// Append the book to the "booksOfGenre" array
let id = doc.documentID
let name = doc["name"] as? String ?? Defaults.defaultName
let featured = doc["featured"] as? Bool ?? Defaults.defaultFeatured
let image = doc["image"] as? String ?? Defaults.defaultImage
let description = doc["description"] as? String ?? Defaults.defaultDescription
let prepTime = doc["prepTime"] as? String ?? Defaults.defaultTime
let cookTime = doc["cookTime"] as? String ?? Defaults.defaultTime
let totalTime = doc["totalTime"] as? String ?? Defaults.defaultTime
let servings = doc["servings"] as? Int ?? Defaults.defaultServings
let highlights = doc["highlights"] as? [String] ?? Defaults.defaultHighlights
let ingredients = [Ingredient]()
let directions = doc["directions"] as? [String] ?? Defaults.defaultDirections
// Append a Recipe with the values
recipes.append(Recipe(id: id, name: name, featured: featured, image: image, description: description, prepTime: prepTime, cookTime: cookTime, totalTime: totalTime, servings: servings, highlights: highlights, ingredients: ingredients, directions: directions))
}
// Output the data
self.data = recipes
}
else {
// Print the error
print(error!.localizedDescription)
}
}
}
I hope this is better.