在UItableview中显示已过滤的Firebase数据

时间:2018-05-08 17:09:11

标签: ios firebase firebase-realtime-database swift3 xcode8

我有一个名为artist的结构,其中包含id,artistName,artistGenre和artistCountry。 Screenshot for clarification. 我想过滤数据并在tableview

中显示artistCountry = "Asgard"的艺术家

这就是我现在正在做的事情:

let query = Database.database().reference().child("artists").queryOrdered(byChild: "artistCountry").queryEqual(toValue: "Asgard")
    query.observe(.value, with:{ (snapshot: DataSnapshot) in
        if snapshot.childrenCount >= 0{
            self.artistsList.removeAll()
        for snap in snapshot.children {
            print((snap as! DataSnapshot).childSnapshot(forPath: "artistName"))
            //getting values
            let artistObject = (snap as AnyObject).value as? [String: AnyObject]
            let artistName = artistObject?["artistName"]
            let artistGenre = artistObject?["artistGenre"]
            let artistCountry = artistObject?["artistCountry"]
            let artistId = artistObject?["id"]
            //creating artists object with model and fetched values
            let artist = artistModel(id: artistId as! String?, name: artistName as! String?, genre: artistGenre as! String?, country: artistCountry as! String?)
            //adding to array
            self.artistsList.append(artist)

        }
            self.tblArtists.reloadData()

        }
    })

使用数组artistList在表格中显示。但是我在let artistObject = (snap as AnyObject).value as? [String: AnyObject] 中收到一条错误,指出“模糊地使用'价值'” 如何将过滤后的数据添加到数组并在tableview中显示?

0 个答案:

没有答案