1

I am working wth a data structure, and I am looping through a couple nodes and here is the json data I get.

Snap (20171012) {
"-KwM45HyW4UduQgKTGn6" =     {
    ImageName = "Screen Shot 2017-10-13 at 11.24.51 AM.png";
    fileURL = "";
    thumbFileUrl = "";
    user = "User not defined";
};
"-KwM4limD2aRyHgeKE5P" =     {
    ImageName = "test.png";
    fileURL = "";
    thumbFileUrl = "";
    user = "User not defined";
};

}

After this, I can access the "snap" value using my data.key to get the "20171012"

ref.child(myselected_spot!).observe(DataEventType.value, with: { (snapshot) in
        if snapshot.childrenCount > 0 {
            for mydata in snapshot.children.allObjects as! [DataSnapshot]
            {
                if mydata.key.characters.count == 8 {
                self.formattedDates.append(convertDate(stringDate: mydata.key))
                self.selected_dates.append(mydata.key)

How would I get the value for "ImageName"

2 Answers 2

2

Your mydata is another DataSnapshot, so you can access all methods and properties of that class. In this case you're looking for DataSnapshot.childSnapshotForPath::

ref.child(myselected_spot!).observe(DataEventType.value, with: { (snapshot) in        if snapshot.childrenCount > 0 {
    for mydata in snapshot.children.allObjects as! [DataSnapshot]
    {
        if mydata.key.characters.count == 8 {
        self.formattedDates.append(convertDate(stringDate: mydata.key))
        self.selected_dates.append(mydata.key)
        print(mydata.childSnapshot(forPath: "ImageName").value)
Sign up to request clarification or add additional context in comments.

1 Comment

So its, close, But I get a <null> printed in the statement
0

Pretty simple - I do not know what the variable myselected_Spot is but I am going to assume it's -KwM45HyW4UduQgKTGn6. If the below code does not yield results - I will need to know what that variable is.

ref.child(myselectd_spot).observe(.value, with: { (snapshot) in
    if snapshot.value is NSNull{
        //handles errors
        return
    }
    else{
        if let selectedSnapDict = snapshot.value as? NSDictionary {//Can also be [String: Any]
            print(selectedSnapDict["ImageName"] as! String) //We know it's a string
        }
        else{
            //null
        }
    }
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.