简体   繁体   English

Firestore 数组到快速结构映射

[英]Firestore array to swift struct mapping

I've been going round in circles trying to figure out how to map a firebase document to a swift struct.我一直在绕圈子试图弄清楚如何将 firebase 文档映射到 swift 结构。 I've got it working well, except I've now added an array within my firebase database and am having zero luck trying to map it to my struct.我让它运行良好,除了我现在在我的 firebase 数据库中添加了一个数组并且尝试将它映射到我的结构时运气为零。

Here is my struct:这是我的结构:

import Foundation
import Firebase

struct subData {

    var userID: String
    var topScore: Int
    var extended: Contents

    init(Doc: DocumentSnapshot) {
        self.userID = Doc.get("userID") as? String ?? "nil"
        self.topScore = Doc.get("topScore") as? Int ?? 0
        self.extended = Contents(data: Doc.get("extendedArray") as! [String: Any])!

    }
}

struct Contents {
    var ID00: Int
    var ID01: Int
    var ID02: Int
    var ID03: Int

    init?(data: [String: Any]) {

        guard let ID00 = data["ID00"] as? Int,
            let ID01 = data["ID01"] as? Int,
            let ID02 = data["ID02"] as? Int,
            let ID03 = data["ID03"] as? Int else {
                return nil
        }

        self.ID00 = ID00
        self.ID01 = ID01
        self.ID02 = ID02
        self.ID03 = ID03
    }
}

Here is my Firestore Database structure:这是我的 Firestore 数据库结构:

在此处输入图片说明

I get the following error:我收到以下错误:

Could not cast value of type '__NSArrayM' (0x1ccc3b660) to 'NSDictionary' (0x1ccc3c628).

I know, I know...the error message is quite clear in what I'm doing wrong - but I've gone round in circles so many times I'm hoping someone can point me in the right direction....我知道,我知道......错误信息很清楚我做错了什么 - 但我已经绕了很多圈,我希望有人能指出我正确的方向......

Thanks.谢谢。

Here extendedArray这里extendedArray

self.extended = Contents(data: Doc.get("extendedArray") as! [String: Any])!

Is an array not a dictionary , so it should be是数组不是字典,所以应该是

Doc.get("extendedArray") as! [[String: Any]]

Use https://github.com/alickbass/CodableFirebase for easy decoding使用https://github.com/alickbass/CodableFirebase轻松解码

According to the model, the below model is right solution,根据模型,下面的模型是正确的解决方案,

struct subData {

    var userID: String
    var topScore: Int
    var extended:[[String:Any]]

    init(Doc: DocumentSnapshot) {
        self.userID = Doc.get("userID") as? String ?? "nil"
        self.topScore = Doc.get("topScore") as? Int ?? 0
        self.extended = Doc.get("extendedArray") as! [[String: Any]]

    }
}

Also Note that if the var extended: NSDictionary?另请注意,如果var extended: NSDictionary? is of kind Dictionary should use self.extended = Doc.get("extendedArray") as? NSDictionary是一种字典应该使用self.extended = Doc.get("extendedArray") as? NSDictionary self.extended = Doc.get("extendedArray") as? NSDictionary instead like wise depends on array or dictionary or any other kind.相反, self.extended = Doc.get("extendedArray") as? NSDictionary明智地依赖于数组或字典或任何其他类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM