Felix F. Felix F. - 3 months ago 11
Swift Question

Retrieve only the Value without the Key in Firebase

So I'm trying to retrieve only the "second" value of a child from my firebase database.
The structure looks like this:

The database Structure

And what I'm getting as an output is this:

What my output is

However what I actually want is just:

someURL.com

Ideally even without the https or http (where I probably need an if to check, since it is not known for all of them in advance). I guess there are 2 options, either unwrapping the string in Swift, or (what I was hoping for) just retrieving the actual value without the brackets and "url" in the beginning. I couldn't find a similar problem on here yet.

My code for retrieving:

dataSource?.populateCellWithBlock { (cell: UITableViewCell, obj: NSObject) -> Void in
let snap = obj as! FIRDataSnapshot


let childString = snap.value as! [String : AnyObject]

cell.textLabel?.text = String(childString)
}

LIH LIH
Answer

Try this

dataSource?.populateCellWithBlock { (cell: UITableViewCell, obj: NSObject) -> Void in
        let snap = obj as! FIRDataSnapshot


        let childString = snap.value as! [String : AnyObject]

        if let url = childString["url"] as? String {
            cell.textLabel?.text = url
        } else {
            print("No value for url")
        }
    }

childString variable you are using is a dictionary which has values for keys. You need to fetch the value for the key "url".