在SwiftUI上的Firebase中使用updateData函数时出错 [英] Error when using updateData function in Firebase on SwiftUI
问题描述
我试图使用updateData(fields:[AnyHashable:Any])函数来更新文档中的数组.一段时间后,我遇到了一个我无法解决的非常奇怪的错误,它工作正常,这是错误消息:
I tried to use the updateData(fields: [AnyHashable : Any]) function to update an array in a document. It was working fine after a while I got a really strange error which I was unable to resolve, here's the error message:
global function '__designTimeString(_:fallback:)' requires that 'AnyHashable' conform to 'ExpressibleByStringLiteral'
----------------------------------------
CompileDylibError: Failed to build Data.swift
Compiling failed: global function '__designTimeString(_:fallback:)' requires that 'AnyHashable' conform to 'ExpressibleByStringLiteral'
Data.swift:115:17: error: global function '__designTimeString(_:fallback:)' requires that 'AnyHashable' conform to 'ExpressibleByStringLiteral'
__designTimeString("#64647.[4].[6].[3].modifier[2].arg[0].value.[0].key.[0].value", fallback: "reviews"): FieldValue.arrayUnion([id])
^
SwiftUI.__designTimeString:1:13: note: where 'T' = 'AnyHashable'
public func __designTimeString<T>(_ key: String, fallback: T) -> T where T : ExpressibleByStringLiteral
我一直在四处逛逛,但实际上找不到有关此类错误的详细信息,请帮助:(
I have been looking around but couldn't really find much details on this kind of errors, please help :(
这是我的代码中导致此错误的部分:
here's the part of my code that caused this error:
db.collection("restaurants").document(restaurantId).updateData([
"reviews": FieldValue.arrayUnion([id])
]) { err in
if let err = err {
print("Error updating document: \(err)")
} else {
print("Document successfully updated")
}
}
推荐答案
使用 updateData
时,键必须为 String
s.
When using updateData
, the keys need to be String
s.
您是否考虑过使用Codable?导入 FirebaseFirestoreSwift
,然后使用 db.collection("restaurants").addDocument(from:review)
保存数据.此处有更多详细信息: SwiftUI:使用以下方法映射Firestore文档Swift Codable
Have you considered using Codable? Import FirebaseFirestoreSwift
, and then save data using db.collection("restaurants").addDocument(from: review)
. More details here: SwiftUI: Mapping Firestore Documents using Swift Codable
这篇关于在SwiftUI上的Firebase中使用updateData函数时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!