【发布时间】:2019-09-12 14:00:25
【问题描述】:
我有一个显示 4 个选项的应用。每天你点击一个或多个选项。现在,它像字符串数组一样存储在 firebase 数据库中,其中每个字符串都是选项之一。像这样
override func addSelection(selection: String) {
self.quitPlan.medications.append(selection)
}
var medications: [String] {
get {
return document[Properties.medications.rawValue] as? [String] ?? []
}
set {
document[Properties.medications.rawValue] = newValue
}
}
但我实际上想要一个 json 数组,带有选项和选项。我试过了:
override func addSelection(selection: String) {
let medicationSelected = Medication(medication: selection, date: Date())
self.quitPlan.medications.append(medicationSelected)
}
var medications: [Medication] {
get {
return document[Properties.medications.rawValue] as? [Medication] ?? []
}
set {
document[Properties.medications.rawValue] = newValue
}
}
struct Medication {
let medication: String
let date: Date
}
但它不起作用,我收到'FIRInvalidArgumentException', reason: 'Unsupported type: __SwiftValue'
【问题讨论】:
-
您能否说明您使用的是 Firebase 实时数据库还是 Firestore?此外,您问题中的任何代码都不会从 Firebase 保存或加载任何内容,因此了解它的外观会很有帮助。最后,包含您预期结构的 sn-p 将帮助我们了解您期望数据库的外观。哦,正如@vadian 在他的回答中提到的那样,您不能在 Firebase 中存储自行创建的结构或类 - 可以存储的内容取决于您使用的 Firebase 产品。