【问题标题】:save arrays of json in firebase with my swift 4 app使用我的 swift 4 应用程序在 firebase 中保存 json 数组
【发布时间】: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 产品。

标签: ios json swift firebase


【解决方案1】:

你可以这样做:

struct Medication {

let medication: String
let date: Date

private let divider = "|"

func toString() -> String {
    return midication + divider + date.toString()
}

func from(_ string: String) -> Medication {
    let arr = string.split(divider)
    let medication = arr[0]
    let date = // TODO: Date from string arr[1]
    return Medication(medication: medication, date: date)
}}

self.quitPlan.medications.append(medicationSelected.toString())

【讨论】:

    【解决方案2】:

    Firebase 无法保存自定义 Swift 结构。

    一种可能的解决方案是将Medication 的数组编码为JSON 字符串。

    struct Medication : Codable {
        let medication: String
        let date: Date
    }
    

    在数据库中将类型从字符串数组更改为单个字符串

    var medications: [Medication] {
        get {
            guard let medicationJSON = document[Properties.medications.rawValue] as? String,
               let data = medicationJSON.data(using: .utf8),
               let medi = try? JSONDecoder().decode([Medication].self, from: data) else { return [] }
            return medi
        }
    
        set {
            let medicationData = try! JSONEncoder().encode(newValue)
            document[Properties.medications.rawValue] = String(data: medicationData, encoding: .utf8)!
        }
    }
    

    【讨论】:

      【解决方案3】:

      Firestore 可以将 Swift 结构保存到集合中,有一个模块可以做到这一点。

      首先,您应该包含模块:

      import FirebaseFirestoreSwift
      

      然后,做:

      db.collection("yourCollectionName").document(from: yourSwiftObject)
      

      它将被转换为保存在您的 Firestore 集合中。

      【讨论】:

        猜你喜欢
        • 2019-05-06
        • 2018-05-01
        • 2020-12-11
        • 1970-01-01
        • 2021-09-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多