【问题标题】:Firebase data into an Array of dictionaries. iOS, SwiftFirebase 数据到字典数组中。 iOS、斯威夫特
【发布时间】:2017-08-13 16:38:37
【问题描述】:

我正在尝试从 firebase 数据库中检索数据并将结果存储在字典数组中。我想以这种方式存储数据....

[[邮编:邮编,纬度:87.28764862,经度:-23.94679475694,注册:注册],[邮编:邮编,纬度:87.28764862,经度:-23.94679475694,注册:注册],[邮编:邮编,纬度:87.28764862 ,经度:-23.94679475694,注册:注册]]

我得到的结果是……

托尼:在附加帖子之前 [] 托尼:发布 [appname.Post]

我哪里错了?

var places = [String]()
var postsNew = [Post]()
var posts = [String: Any]()

DataService.ds.REF_POSTS.observe(.value, with: { (snapshot) in
        if snapshot.hasChild("postCodes") {
            let refToUser = DataService.ds.REF_POSTS
            refToUser.child("postCodes").observe(.value, with: { snapshot in

                let value = snapshot.value as? NSDictionary
                let postsIds = value?.allKeys as! [String]
                for postId in postsIds {
                    let refToPost = Database.database().reference(withPath: "posts/" + "postCodes/" + postId)
                    refToPost.observe(.value, with: { snapshot in
                        if snapshot.exists() {

                            let postDict = snapshot.value as? Dictionary<String, AnyObject>
                            let pCodeData = snapshot.key
                            let post = Post(postKey: pCodeData, postData: postDict!)


                            self.configureCell(post: post)
                            print("Tony: before append post \(self.postsNew)")
                            self.postsNew.append(post)
                            print("Tony: post \(self.postsNew)")


                }else {
                            print("Tony: Couldn't get the data")
                        }
                    })
                }
            })
        }else {
            print("Tony: No Favs added, couldn't get the data")
        }

    })


func configureCell(post: Post) {
    self.postData = post

    self.latitude = post.latitude
    self.longitude = post.longitude
    self.postCodeDB = post.postCode
    self.registration = post.other2

}

在我的 Post 类中..

    init(postKey: String, postData: Dictionary<String, AnyObject>) {
    self._postKey = postKey

我的 Firebase 数据...

如果我向我的 firebase 添加另一个,我会得到这个结果...

【问题讨论】:

    标签: swift dictionary firebase firebase-realtime-database


    【解决方案1】:

    我通过查看我的代码找到了答案。

    DataService.ds.REF_POSTS.child("postCodes").observe(.value, with: { (snapshot) in
    
                    let value = snapshot.value as? NSDictionary
                    let postsIds = value?.allKeys as! [String]
                    for postId in postsIds {
                        let refToPost = Database.database().reference(withPath: "posts/" + "postCodes/" + postId)
                        refToPost.observe(.value, with: { snapshot in
                            if snapshot.exists() {
    
                                let postDict = snapshot.value as? [String: AnyObject]
    
                                print("Tony: before append post \(self.posts)")
                                self.posts.append(postDict!)
                                print("Tony: post \(self.posts)")
                                self.showSightingsOnMap()
    
                    }else {
                                print("Tony: Couldn't get the data")
                            }
                        })
                    }
                })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-22
      • 2017-03-24
      • 2023-03-17
      • 1970-01-01
      • 2019-08-02
      • 2020-07-31
      相关资源
      最近更新 更多