繁体   English   中英

为什么会收到错误消息“无法将类型 &#39;[Cocktail]&#39; 的值转换为预期的参数类型 &#39;Binding<Data> ”

[英]Why am getting the error message "Cannot convert value of type '[Cocktail]' to expected argument type 'Binding<Data>"

我有一个 SwiftUI 项目 (Xcode 13 SwiftUI 3.0),我试图在其中显示 SwiftUI 列表中的鸡尾酒成分列表,但我对 SwiftUI 不是很熟悉,我不知道为什么会收到此错误消息。 这是我的代码:

class NetworkManager: ObservableObject {
    
    @Published var cocktails = [Cocktail]()
    
    func fetchData(_ urlString: String) {
        guard let url = URL(string: urlString) else { return }
        let session = URLSession(configuration: .default)
        let task = session.dataTask(with: url) { data, response, error in
            if error == nil {
                let decoder = JSONDecoder()
                guard let safeData = data else { return }
                do {
                    let results = try decoder.decode(Drinks.self, from: safeData)
                    DispatchQueue.main.async {
                        self.cocktails = results.drinks
                        self.buildIngredients()
                    }
                } catch let error {
                    print(error)
                }
            }
        }
        task.resume()
    }
    
    func buildIngredients() {
        
        for index in 0..<cocktails.count {
            if let ingredient1 = cocktails[index].strIngredient1 {
                cocktails[index].Ingredients.list.append(ingredient1)
            }
            if let ingredient2 = cocktails[index].strIngredient2 {
                cocktails[index].Ingredients.list.append(ingredient2)
            }
            if let ingredient3 = cocktails[index].strIngredient3 {
                cocktails[index].Ingredients.list.append(ingredient3)
            }
            if let ingredient4 = cocktails[index].strIngredient4 {
                cocktails[index].Ingredients.list.append(ingredient4)
            }
            if let ingredient5 = cocktails[index].strIngredient5 {
                cocktails[index].ingredients.append(ingredient5)
            }
            if let ingredient6 = cocktails[index].strIngredient6 {
                cocktails[index].ingredients.append(ingredient6)
            }
            if let ingredient7 = cocktails[index].strIngredient7 {
                cocktails[index].ingredients.append(ingredient7)
            }
            if let ingredient8 = cocktails[index].strIngredient8 {
                cocktails[index].ingredients.append(ingredient8)
            }
            if let ingredient9 = cocktails[index].strIngredient9 {
                cocktails[index].ingredients.append(ingredient9)
            }
            if let ingredient10 = cocktails[index].strIngredient10 {
                cocktails[index].ingredients.append(ingredient10)
            }
            if let ingredient11 = cocktails[index].strIngredient11 {
                cocktails[index].ingredients.append(ingredient11)
            }
            if let ingredient12 = cocktails[index].strIngredient12 {
                cocktails[index].ingredients.append(ingredient12)
            }

        }
    }

}

struct Drinks: Decodable {
    var drinks: [Cocktail]
}

struct Cocktail: Decodable, Identifiable {
    var id: String {
        return idDrink
    }
    let idDrink: String
    let strDrink: String
    let strDrinkThumb: String
    let strAlcoholic: String
    let strGlass: String
    let strInstructions: String
    let strIngredient1: String?
    let strIngredient2: String?
    let strIngredient3: String?
    let strIngredient4: String?
    let strIngredient5: String?
    let strIngredient6: String?
    let strIngredient7: String?
    let strIngredient8: String?
    let strIngredient9: String?
    let strIngredient10: String?
    let strIngredient11: String?
    let strIngredient12: String?
    struct Ingredients: Identifiable {
        let id = UUID()
        var list: [String]
    }
}

struct DetailsView: View {
    
    @ObservedObject var networkManager = NetworkManager()
    
    let urlString: String
    
    var body: some View {
        List(networkManager.cocktails) { cocktail in
            VStack(alignment: .center) {
                HStack(alignment: .center) {
                    Text(cocktail.strDrink + "  -")
                        .navigationTitle("Cocktail by first letter")
                        .frame(alignment: .center)
                    Text(cocktail.strAlcoholic)
                        .frame(alignment: .center)
                }
                
                WebImage(url: URL(string: cocktail.strDrinkThumb))
                    .resizable()
                    .frame(width: 200, height: 200, alignment: .center)
                
                Text("Ingredients List").frame(alignment: .center)
                
                List(networkManager.cocktails, id: \.self) { cocktail in
                    ForEach(cocktail.Ingredients.list) { ingredient in
                        Text(ingredient)
                    }
                }
            }
        }
        .onAppear {
            self.networkManager.fetchData(urlString)
        }
    }
}

错误信息

我曾尝试将属性包装器@State添加到 Structs 及其属性中,但没有成功

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM