I’m parsing data from a source that’s structured like this:
I’m able to parse all data within the array, but how can I save all items of the parsed array to save into Firestore? This is what I have right now:
let decoder = JSONDecoder()
let recipeToDisplay = try decoder.decode(Recipe.self, from: data!) // Issue that there is no @DocumentID from Spoonacular?
let uuid = UUID().uuidString
SourceToFirestoreService.createRecipe(
docId:uuid,
instructionsAnalyzedName: recipeToDisplay.analyzedInstructions?[0].name ?? "",
instructionsAnalyzedStepsNumber: recipeToDisplay.analyzedInstructions?[0].steps[0]?.number ?? 0,
instructionsAnalyzedStepsStep: recipeToDisplay.analyzedInstructions?[0].steps[0]?.step ?? ""
) { recipeURL in
print("success")
}
class SourceToFirestoreService {
static func createRecipe(
instructionsAnalyzedName:String,
instructionsAnalyzedStepsNumber:Int,
instructionsAnalyzedStepsStep:String,
completion: @escaping (Recipe?) -> Void) {
let db = Firestore.firestore()
db.collection("recipes").document(docId).setData(
[
"analyzedInstructions": [
["name": instructionsAnalyzedName,
"steps": [
["number": instructionsAnalyzedStepsNumber,
"step": instructionsAnalyzedStepsStep],
],
],
],
]) { (error) in
}
}
}
As you can see, I’m specifying the array index in recipeToDisplay.analyzedInstructions?[0].name
, recipeToDisplay.analyzedInstructions?[0].steps[0]?.number
, and recipeToDisplay.analyzedInstructions?[0].steps[0]?.step
. But what if I want to parse every item in those arrays and save to Firestore?
Any guidance is much appreciated!