使用Swift解码实时数据库响应
我正在尝试解码Firebase实时数据库响应,但遇到解码错误,我认为这不是我的结构是问题所在,但我不知道我要去哪里做错了,我在做什么错?
现在的问题似乎是 promocodused
是字典而不是数组
ERROR HERE: typeMismatch(Swift.Array<Any>, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "usedBy", intValue: nil)], debugDescription: "Expected to decode Array<Any> but found a dictionary instead.", underlyingError: nil))
代码:
func getPromoCodeInfo(handler: @escaping (PromoCode?) -> ()) {
guard let userid = AuthService.shared.userID else { return }
promoCodes.queryOrdered(byChild: "owner").queryEqual(toValue: userid).observeSingleEvent(of: .value) { snapshot in
for child in snapshot.children {
guard let snap = child as? DataSnapshot else { return }
guard let value = snap.value as? [String: Any] else { return }
do {
let jsonData = try JSONSerialization.data(withJSONObject: value, options: [])
let decoded = try JSONDecoder().decode(PromoCode.self, from: jsonData)
handler(decoded)
} catch {
print("ERROR HERE: \(error)")
}
}
}
}
datamodel
struct PromoCode: Codable {
var active: Int
var amount: Int?
var code: String
var percent: Int?
var owner: Int?
var usedBy: [PromoCodeUsed]?
}
struct PromoCodeUsed: Codable {
var amount: Int
var date: String
var paid: Int
var userid: Int
}
数据
{
"active": 1,
"code": "FELLA",
"owner": 33206,
"percent": 50,
"usedBy": {
"-N5lHtnKtAiBWrQMxW0a": {
"amount": 1000,
"date": "2022-06-29 21:08:51 +0000",
"paid": false,
"userid": 33206
},
"-N6L0vybeecuu175FV-4": {
"amount": 5000,
"date": "2022-07-07 00:20:41 +0000",
"paid": false,
"userid": 33206
},
"-N6L0w5WVS6mkVGrfJMW": {
"amount": 5000,
"date": "2022-07-07 00:20:41 +0000",
"paid": false,
"userid": 33206
},
"-N6L190TsMr6OJAXod1G": {
"amount": 1000,
"date": "2022-07-07 00:21:38 +0000",
"paid": false,
"userid": 33206
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您针对Firebase数据库执行查询时,可能会有多个结果。因此,快照包含这些结果的列表。即使只有一个结果,快照也将包含一个结果的列表。
您的代码需要处理此列表,但没有这样做。处理列表的最简单方法是您得到了,请循环过
snapshot.Children
,然后将每个单独的孩子快照转换为promocode
对象。When you execute a query against the Firebase Database, there will potentially be multiple results. So the snapshot contains a list of those results. Even if there is only a single result, the snapshot will contain a list of one result.
Your code needs to handle this list, but fails to do so. The easiest way to handle the list is to loop over the children of the snapshot you get, so loop over
snapshot.children
and then convert each individual child snapshot to aPromoCode
object.