简体   繁体   English

Swift 错误:表达式类型不明确,没有更多上下文

[英]Swift Error: Type of expression is ambiguous without more context

Im trying to make a Zomato clone in SwiftUI. The error occurs when I try to loop through an item in the Firebase Firestore DB.我试图在 SwiftUI 中制作 Zomato 克隆。当我尝试循环访问 Firebase Firestore 数据库中的项目时发生错误。 The error occurs in this code:错误发生在这段代码中:

let product = Product(name: productName, price: productPrice)

Here's the entire code: https://github.com/MysteryCoder456/VegieMato/blob/backend/VegieMato/TabViews/HomeView.swift#L57这是完整的代码: https://github.com/MysteryCoder456/VegieMato/blob/backend/VegieMato/TabViews/HomeView.swift#L57

Your productObjects is declared as a let constant and needs to be a var if you want to append items to it.您的productObjects被声明为一个let常量,如果您想要 append 个项目,则需要是一个var

let productObjects: Array<Product> = [] // <--- Change to a var
    for prod in vendorProducts {
        let productName = prod["name"]
        let productPrice = prod["price"]
        let product = Product(name: productName, price: productPrice)
        productObjects.append(product)
}

Your productName and productPrice values would be interpreted as Any data type since the prod value has been defined as [String:Any].您的 productName 和 productPrice 值将被解释为 Any 数据类型,因为 prod 值已定义为 [String:Any]。 So it has to properly casted before creating Product instance.所以它必须在创建 Product 实例之前正确转换。

if let productName = prod[“name”] as? String, 
   let productPrice = prod[“price”] as? Int {
   let product = Product(name: productName, price: productPrice)
   productObjects.append(product) 
}

暂无
暂无

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

相关问题 从 Firebase 具有特定 ID 的文档中提取时,表达式类型不明确,没有更多上下文,映射到 Codable - Error Type of expression is ambiguous without more context when pulling from Firebase Document with specific ID, mapping to Codable 从 Firebase 获取时出现错误“表达式类型不明确,没有更多上下文” - Error 'Type of expression is ambiguous without more context' when fetching from Firebase Firestore 和 Swift UI 表达式类型不明确 - Firestore and Swift UI Type of expression is ambiguous 我在 context 上有错误。参数类型“Context”不能分配给参数类型“BuildContext” - I have error on context..The argument type 'Context' can't be assigned to the parameter type 'BuildContext' 错误:[dart] 无法将参数类型“Context”分配给参数类型“BuildContext”。 [argument_type_not_assignable] - error: [dart] The argument type 'Context' can't be assigned to the parameter type 'BuildContext'. [argument_type_not_assignable] Swift 预处理错误:预处理器表达式中的预期行结束 - Swift Preprocessing Error: Expected end of line in preprocessor expression Terraform 错误字符串类型的值不能用作“for”表达式中的集合 - Terraform error A value of type string cannot be used as the collection in a 'for' expression 在 Dialogflow 中不使用 webhook 将参数添加到上下文 - Add parameter to context without webhook in Dialogflow 如何使用 typescript 在反应上下文中键入承诺 - How to type promises on react context with typescript 尝试在 swift 应用程序的 Firestore 中添加数组元素时出现“无法将 'String' 类型的值转换为预期的参数类型 '[Any]'”错误 - "Cannot convert value of type 'String' to expected argument type '[Any]'" error when trying to add array element in firestore in swift app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM