如何将对象数组插入 Firestore

问题描述 投票:0回答:1

我想将对象数组作为字段插入 Firestore 文档中。我拥有的是:

struct Race {
    ...other properties
    results: [RunnerResult]
}
struct RunnerResult { ... }

func saveRace(race: Race) {

    database.collection("Races").document(race.id).setData(
         ...other properties
         results: race.results,
    )
 }

但是每当它尝试保存并出现错误

results: race.results
时,它就会在线
Unsupported type: __SwiftValue
上崩溃。是否可以将对象数组直接保存到 Firestore 中,还是需要操作数据?

swift firebase google-cloud-firestore
1个回答
0
投票

您的意思可能是这样的吗(这是一个加密示例(始终隐藏您的密钥。这些只是编造的......))?

  let db = Firestore.firestore()
    @IBAction func SaveDataSet1(_ sender: UIButton) {
        Task { @MainActor in

            let key256 = "09754896534186486564891846888562"
            
            let iv = "mznfojdlaougnhu"
            
            let aes256 = AES(key: key256, iv: iv)
            
            let encryptedFirstName = aes256?.encrypt(string: textfieldforFIRSTNAME.text!)
            
            
            
            db.collection("Races").addDocument(data: [
                "First name": encryptedFirstName
            ]) { (error) in
                if error != nil {
                    print("There was an issue saving the data", error!)
                } else {
                    print("Success!")
                }
            }
        }
© www.soinside.com 2019 - 2024. All rights reserved.