将Firebase数据转换为字典数组。 iOS,Swift

时间:2017-08-13 16:38:37

标签: swift dictionary firebase firebase-realtime-database

我试图从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 data

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

1 个答案:

答案 0 :(得分:2)

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

ColorStateList