简体   繁体   English

Swift / Firestore - 如何获取包含嵌套 map 对象的单个文档并将它们发送到结构?

[英]Swift / Firestore - How do I get a single document with nested map objects and send them to a Struct?

I'm trying to get my data from a single document into a Struct for a Tableview.我正在尝试将我的数据从单个文档中获取到 Tableview 的 Struct 中。 For some reason when getting a single document from Firestore.出于某种原因,从 Firestore 获取单个文档时。 It comes back as a key-value pair.它以键值对的形式返回。 I thought it would be a dictionary.我以为它会是一本字典。 The same as when you fetch all documents from a collection.与从集合中获取所有文档时相同。 I have several nested maps(photo) inside of a map(photos).我在地图(照片)中有几个嵌套的地图(照片)。 How do I get the nested maps and add the individual items into my Struct?如何获取嵌套地图并将单个项目添加到我的结构中?

Firestore 文档

var items: [Item] = [] 
db.collection("items").document("aAZWjpXwo2V05rMOsQjR")
     .getDocument{ (docSnapshot, err) in
     if let err = err {
         print("Error getting documents: \(err)")
     } else {
        for document in docSnapshot!.data()! {
            print("\(document)")
            if let item = Item(dictionary: document){
                 self.items.append(item)
            }
        }
     }
}
struct Item {

    var photoUrl: String
    var item_id: String

    init(photoUrl: String, item_id: String) {
        self.photoUrl = photoUrl
        self.item_id = item_id
    }

    // I don't know how to use key-value pairs in Swift
    // init?(dictionary: (key: String, value: Any)) {

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

        for photoInfo in dictionary["photos"] {

            let photoData = photoInfo["photo"] as? [String: Any]
            let photoUrl = photoData!["photoUrl"] as! String
            let item_id = photoData!["item_id"] as! String
        }

        self.init(photoUrl: photoUrl!, item_id: item_id!)
    }
}

I was able to get the embedded map items this way.我能够以这种方式获得嵌入式 map 项目。 I used [[String: Any]] to fix my issue.我使用 [[String: Any]] 来解决我的问题。 I guess an embedded map is treated as an array inside of an array.我猜嵌入式 map 被视为数组内部的数组。 I'm still learning Swift.我还在学习 Swift。 It took me over a week to figure this out.我花了一个多星期才弄清楚这一点。 Happy coding everyone...祝大家编码愉快...

var items: [Item] = [] 
db.collection("items").document("aAZWjpXwo2V05rMOsQjR")
     .getDocument{ (docSnapshot, err) in
     if let err = err {
         print("Error getting documents: \(err)")
     } else {
        if let document = docSnapshot.data(),
            let doc = document["photos"] as? [[String: Any]] {
            for photoInfo in doc {
                if let item = Item(dictionary: photoInfo){
                    self.items.append(item)
                }
            }
     }
}

struct Item {

    var photoUrl: String
    var item_id: String

    init(photoUrl: String, item_id: String) {
        self.photoUrl = photoUrl
        self.item_id = item_id
    }

    // I don't know how to use key-value pairs in Swift
    // init?(dictionary: (key: String, value: Any)) {

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

        let photoData = dictionary["photo"] as? [String: Any]
        let photoUrl = photoData!["photoUrl"] as! String
        let item_id = photoData!["item_id"] as! String

        self.init(photoUrl: photoUrl!, item_id: item_id!)
    }
}

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

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