gpt4 book ai didi

swift - Firebase 数据到字典数组中。 iOS, swift

转载 作者:搜寻专家 更新时间:2023-10-31 19:32:38 28 4
gpt4 key购买 nike

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

[[ zip : zip ,纬度:87.28764862,经度:-23.94679475694,登记:登记],[ zip : zip ,纬度:87.28764862,经度:-23.94679475694,登记:登记],[ zip : zip ,纬度:87.28764862 ,经度:-23.94679475694,注册:注册]]

我得到的结果是...

Tony: before append post [] Tony: post [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 data

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

最佳答案

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

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")
}
})
}
})

关于swift - Firebase 数据到字典数组中。 iOS, swift ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45662586/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com