Error retrieving Data from Firebase - Swift 4 XCode 9

225 views
Skip to first unread message

Ibrahim Akar

unread,
Sep 29, 2017, 3:07:11 PM9/29/17
to Firebase Google Group


I have attached a screen shot of what my data looks like. I am trying to get the list of titles (child node) and populate my table field with same list.

My errors and explanation are highlighted below in the code. 

Appreciate any help. 


//Set the Firebase Reference
    var ref = Database.database().reference()
    
    var searchJSONQuery : String = ""
    
    var jobsData = [String]()
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return jobsData.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "PostCell")
        cell?.textLabel?.text = jobsData[indexPath.row]
        return cell!
    }
    
    func retrieveJobTitles(){
        
        let positionRef = ref.child("positions")
        
        positionRef.observeSingleEvent(of: .value, with: { (snapshot) in
            
            // Iterate through all of your positions
            for child in snapshot.children {
                let positionsInfo = child.value as! [String: Any] 
 
Error -->  Value of type 'Any' has no member 'value'
             Cast 'Any' to 'AnyObject' or use 'as!' to force downcast to a more specific type to access members
When I use AnyObject as the error message suggests I just run into another one .. Value of type 'Any' has no member 'value'
                
                let jobTitle = positionsInfo["title"] as! String
                
                if jobTitle != nil {
                    self.jobsData.append(jobTitle)
                }
            }
            
            self.jobPostsTableView.reloadData()
            
            
        })
    }
jobsDataImage.png

Jen Person

unread,
Sep 29, 2017, 9:19:01 PM9/29/17
to Firebase Google Group
`snapshot.children` needs to explicitly be cast to `DataSnapshot`. Try :
for child in snapshot.children.allObjects as! [DataSnapshot] {
// code here
}
Reply all
Reply to author
Forward
0 new messages