ami*_*t17 5 json ios firebase swift google-cloud-firestore
我有以下内容struct
:
struct Recipe: Codable {
@DocumentID var id: String?
var vegetarian: Bool?
}
Run Code Online (Sandbox Code Playgroud)
这就是我解析 Firestore 数据的方式:
do {
let decoder = JSONDecoder()
let recipeToDisplay = try decoder.decode(Recipe.self, from: data!)
let uuid = UUID().uuidString
FirestoreService.createRecipe(
documentId: uuid,
vegetarian: recipeToDisplay.vegetarian ?? false
) { recipeURL in
print("success")
}
} catch {
print("Error parsing response data: \(error)")
}
Run Code Online (Sandbox Code Playgroud)
该catch
声明被调用,我收到以下错误消息:decodingIsNotSupported("DocumentID values can only be decoded with Firestore.Decoder")
。
我研究过的所有文档都指出我要使用它来JSONDecoder()
解析数据,但我在Firestore.Decoder
. 我应该有不同的方式来解析数据吗?
问题是我试图id
从没有属性的源进行解码id
。id
从我CodingKeys
解决的问题中排除。
struct Recipe: Codable {
@DocumentID var id: String?
var vegetarian: Bool?
private enum CodingKeys: String, CodingKey {
case id
case vegetarian
}
}
Run Code Online (Sandbox Code Playgroud)