【发布时间】: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 添加另一个,我会得到这个结果...
【问题讨论】:
标签: swift dictionary firebase firebase-realtime-database