I'm trying to fetch two APIs in SwiftUI, the first Api works great, but when I try to fetch the second one I only get coding keys error.
first API is: "https://playground.devskills.co/api/rest/meal-roulette-app/meals"
What did work with the first API:
second API: "https://playground.devskills.co/api/rest/meal-roulette-app/meals/(id)" this is what I tried to do with the second api
This is the error I get:
Here is the code that docent work:
import SwiftUI
struct RecipeView: View {
@State var recipeId: Int
@State var data = SpecificMealData(
specificMealRoulette:
SpecificMealRoulette(
id: 0,
title: "Default Title",
picture: "Default Image",
description: "Default Description",
Ingredients: "Default Ingredients"))
var body: some View {
VStack {
Text(data.specificMealRoulette.title)
}.onAppear{
getData(id: recipeId)
}
}
func getData(id: Int) {
print("TAG Entered ID I FUNC")
let urlString = "https://playground.devskills.co/api/rest/meal-roulette-app/meals/\(4)"
let url = URL(string: urlString)
URLSession.shared.dataTask(with: url!) {data, _ , error in
DispatchQueue.main.async {
if let data = data {
do {
let decoder = JSONDecoder()
let decodedData = try decoder.decode(SpecificMealData.self, from: data)
self.data = decodedData
} catch {
print("TAG Error!", error)
}
}
}
}.resume()
}
}
//This is another file/class/struct
import Foundation
struct MealsData: Decodable {
var mealRoulette: [MealRoulette]
enum CodingKeys: String, CodingKey {
case mealRoulette = "meal_roulette_app_meals"
}
}
struct MealRoulette: Decodable, Hashable {
var id: Int
var title: String
var picture: String
}
struct SpecificMealData: Decodable {
var specificMealRoulette: SpecificMealRoulette
enum CodingKeys: String, CodingKey {
case specificMealRoulette = "meal_roulette_app_meals_by_pk"
}
}
struct SpecificMealRoulette: Decodable {
var id: Int
var title: String
var picture: String
var description: String
var Ingredients: String
}
this is the API I want to fetch:
{
"meal_roulette_app_meals_by_pk": {
"id": 4,
"title": "Thai fried prawn & pineapple rice",
"picture": "https://firebasestorage.googleapis.com/v0/b/devskills-prod.appspot.com/o/Thai fried prawn & pineapple rice.webp?alt=media&token=268c55fc-e977-496c-be36-b99d9e6eba04",
"description": "This quick, low calorie supper is perfect for a busy weeknight. Cook your rice in advance to get ahead - run it under cold water to chill quickly, then freeze in a food bag for up to one month.",
"ingredients": "2 tsp sunflower oil,\nbunch spring onions greens and whites separated both sliced,\n1 green pepper deseeded and chopped into small chunks,\n140g pineapple chopped into bite-sized chunks,\n3 tbsp Thai green curry paste,\n4 tsp light soy sauce plus extra to serve,\n300g cooked basmati rice (brown white or a mix - about 140g uncooked rice),\n2 large eggs beaten,\n140g frozen peas,\n225g can bamboo shoots drained,\n250g frozen prawns cooked or raw,\n2-3 limes 1 juiced the rest cut into wedges to serve,\nhandful coriander leaves (optional)"
}
}
Here is the error I get:
Error! keyNotFound(CodingKeys(stringValue: "Ingredients", intValue: nil), Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "meal_roulette_app_meals_by_pk", intValue: nil)], debugDescription: "No value associated with key CodingKeys(stringValue: "Ingredients", intValue: nil) ("Ingredients").", underlyingError: nil))
Please be nice, this is my first time working with APIs in SwiftUI :)
CodePudding user response:
Change your model to this
struct SpecificMealRoulette: Decodable {
var id: Int
var title: String
var picture: String
var description: String
var ingredients: String
}