Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 从Firebase打印出一些子项_Ios_Swift_Xcode_Firebase - Fatal编程技术网

Ios 从Firebase打印出一些子项

Ios 从Firebase打印出一些子项,ios,swift,xcode,firebase,Ios,Swift,Xcode,Firebase,我想显示Firebase中的一些数据。我有一个节点,它有一堆名为posts的帖子 Database.database().reference().child("main").child("posts") 使用上面的代码可以打印posts节点中的所有帖子。然而,我不想打印出所有的帖子,我想打印出一些帖子。 我想根据帖子id打印一些帖子,如何打印多篇帖子 编辑 var postingID: String? var posts = NSMutableArray() func loadDetai

我想显示Firebase中的一些数据。我有一个节点,它有一堆名为posts的帖子

Database.database().reference().child("main").child("posts")
使用上面的代码可以打印posts节点中的所有帖子。然而,我不想打印出所有的帖子,我想打印出一些帖子。 我想根据帖子id打印一些帖子,如何打印多篇帖子

编辑

var postingID: String?
var posts = NSMutableArray()

  func loadDetails(){
    Database.database().reference().child("main").child("users").child(screenName!).child("bookmarks").observeSingleEvent(of: .value, with: { (snapshot:DataSnapshot) in


        if let postsDictionary = snapshot .value as? [String: AnyObject] {


             for testingkey in postsDictionary.keys {
               Database.database().reference().child("main").child("posts").queryOrderedByKey().observeSingleEvent(of: .value, with: { (snapshot) in

                    if snapshot.exists() {
                        for child in snapshot.children {
                            let snap = child as! DataSnapshot
                            if snap.hasChildren() {
                                if let id = snap.childSnapshot(forPath: "id").value as? String, id == testingkey {
                                    print(snap.value!)
                                    self.posts.insert(snap.value!, at: 0)
                                }
                            }
                        }
                    }

                     })
        }

        }})

}
编辑

var postingID: String?
var posts = NSMutableArray()

  func loadDetails(){
    Database.database().reference().child("main").child("users").child(screenName!).child("bookmarks").observeSingleEvent(of: .value, with: { (snapshot:DataSnapshot) in


        if let postsDictionary = snapshot .value as? [String: AnyObject] {


             for testingkey in postsDictionary.keys {
               Database.database().reference().child("main").child("posts").queryOrderedByKey().observeSingleEvent(of: .value, with: { (snapshot) in

                    if snapshot.exists() {
                        for child in snapshot.children {
                            let snap = child as! DataSnapshot
                            if snap.hasChildren() {
                                if let id = snap.childSnapshot(forPath: "id").value as? String, id == testingkey {
                                    print(snap.value!)
                                    self.posts.insert(snap.value!, at: 0)
                                }
                            }
                        }
                    }

                     })
        }

        }})

}
您一直在这里设置相同的字符串

self.postingID=测试键

因此它等于for循环中的最后一个元素。因此,在您的if条件下

id==self.postingID

仅对最后一个节点为true

也许您希望更改数组中的self.postingID(如果id不唯一,则设置),并具有以下内容:

var self.postingIdArray = [String]()
然后在第一个函数中,要将每个键附加到此数组

for testingkey in postsDictionary.keys {

        self.postingID = testingkey

    }
最后,您需要检查节点的id是否在数组中,如

if let id = snap.childSnapshot(forPath: "id").value as? String, self.postingIdArray.contains(id) { self.posts.insert(snap, at: 0) }
旧答案

let databaseRef = Database.database().reference().child("main")
let query = databaseRef.queryOrderedByKey()

query.observeSingleEvent(of: .value) { (snapshot) in
        if snapshot.exists() {
            for child in snapshot.children {
                let snap = child as! DataSnapshot
                if snap.hasChildren() {
                    if let id = snap.childSnapshot(forPath: "postId").value as? String, id == idToCompare {
                            print(id)
                    }
                }
            }
        }
    }
编辑

var postingID: String?
var posts = NSMutableArray()

  func loadDetails(){
    Database.database().reference().child("main").child("users").child(screenName!).child("bookmarks").observeSingleEvent(of: .value, with: { (snapshot:DataSnapshot) in


        if let postsDictionary = snapshot .value as? [String: AnyObject] {


             for testingkey in postsDictionary.keys {
               Database.database().reference().child("main").child("posts").queryOrderedByKey().observeSingleEvent(of: .value, with: { (snapshot) in

                    if snapshot.exists() {
                        for child in snapshot.children {
                            let snap = child as! DataSnapshot
                            if snap.hasChildren() {
                                if let id = snap.childSnapshot(forPath: "id").value as? String, id == testingkey {
                                    print(snap.value!)
                                    self.posts.insert(snap.value!, at: 0)
                                }
                            }
                        }
                    }

                     })
        }

        }})

}
您一直在这里设置相同的字符串

self.postingID=测试键

因此它等于for循环中的最后一个元素。因此,在您的if条件下

id==self.postingID

仅对最后一个节点为true

也许您希望更改数组中的self.postingID(如果id不唯一,则设置),并具有以下内容:

var self.postingIdArray = [String]()
然后在第一个函数中,要将每个键附加到此数组

for testingkey in postsDictionary.keys {

        self.postingID = testingkey

    }
最后,您需要检查节点的id是否在数组中,如

if let id = snap.childSnapshot(forPath: "id").value as? String, self.postingIdArray.contains(id) { self.posts.insert(snap, at: 0) }
旧答案

let databaseRef = Database.database().reference().child("main")
let query = databaseRef.queryOrderedByKey()

query.observeSingleEvent(of: .value) { (snapshot) in
        if snapshot.exists() {
            for child in snapshot.children {
                let snap = child as! DataSnapshot
                if snap.hasChildren() {
                    if let id = snap.childSnapshot(forPath: "postId").value as? String, id == idToCompare {
                            print(id)
                    }
                }
            }
        }
    }

它会打印出我想要的ID,但如何获取这些节点的值?child是指节点。试着打印child而不是id,让我知道print(child)只打印最后一张帖子我有四张帖子我想打印post1…post4,它只打印post4的快照post4是唯一有孩子的吗?如果是这样,请删除最后一个If条件。它打印出我想要的ID,但如何获取这些节点的值?child是指节点。试着打印child而不是id,让我知道print(child)只打印最后一张帖子我有四张帖子我想打印post1…post4,它只打印post4的快照post4是唯一有孩子的吗?如果是这样,请删除最后一个If条件