首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >KeyNotFound(CodinkgKeys)获取API SwiftUI时

KeyNotFound(CodinkgKeys)获取API SwiftUI时
EN

Stack Overflow用户
提问于 2021-12-30 11:03:32
回答 1查看 90关注 0票数 0

我试图在SwiftUI中获取两个Api,第一个Api工作得很好,但是当我尝试获取第二个Api时,我只得到编码键错误。

第一个API是:"https://playground.devskills.co/api/rest/meal-roulette-app/meals

下面是剂量工作的代码:

代码语言:javascript
复制
    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()
    }
}

//这是另一个文件/class/struct

代码语言:javascript
复制
    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
}

这是我想要获取的API:

代码语言:javascript
复制
    {
    "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%20fried%20prawn%20%26%20pineapple%20rice.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)"
    }
}

下面是我遇到的错误:

错误!keyNotFound(CodingKeys:“成分”,intValue:“0”),Swift.DecodingError.Context(codingPath: CodingKeys(stringValue:"meal_roulette_app_meals_by_pk",intValue: nil),debugDescription:“无与键相关联的值”(stringValue:"Ingredients",intValue: nil) (“成分”)。“,underlyingError: 0”)

拜托,这是我第一次在SwiftUI中使用API :)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-12-30 11:56:01

将您的模型更改为

代码语言:javascript
复制
struct SpecificMealRoulette: Decodable {
     var id: Int
     var title: String
     var picture: String
     var description: String
     var ingredients: String
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70530729

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档