Learn Courses My Dashboard

How to extract data that's a custom data type?

I’m extracting the following data via an API:

{
  "vegetarian": true,
  "vegan": true,
  "glutenFree": false,
  "extendedIngredients": [
   {
    "name": "sugar",
    "amount": 2,
    "unit": "tablespoons",
    "measures": {
      "us": {
        "amount": 2
        "unit": "tablespoons"
      }
      "metric": {
        "amount": 2
        "unit": "tablespoons"
      }
    }
   }
  ]
}

These are my custom structs to handle this data:

struct Recipe: Codable {
    var vegetarian: Bool?
    var vegan: Bool?
    var glutenFree: Bool?
    var extendedIngredients: [ExtendedIngredients?]
}

struct ExtendedIngredients: Codable {
    var name: String?
    var amount: Double?
    var unit: String?
}

I’ve created a function to save the data into Firestore (the parameters come from a different view controller):

    static func createRecipe(vegetarian:Bool, vegan:Bool, glutenFree:Bool, completion: @escaping (RecipeURL?) -> Void) {
       
        let db = Firestore.firestore()
        
        db.collection("recipes").document("\(documentID)").setData(
            ["vegetarian":vegetarian,
             "vegan":vegan,
             "glutenFree":glutenFree,
            ]) { (error) in
             }
    }

I’ve had no issue saving the simple data types (e.g., vegetarian), but how can I go about saving the custom data type of ExtendedIngredients? I’m not sure how to include it in my function.