3

I am currently trying to access data from a child snapshot in Swift. Here is my code that I have (which worked before the Swift 3/Firebase update):

 if let achievements = snapshot1.childSnapshotForPath("Achievements").children.allObjects as? [FIRDataSnapshot] {


                    if achievements.count != 0 {

                        if let val = achievements[0].value!["somevalue"] as? Int {
                            self.dict["somevalue"] = val
                        }


                    }

So what I am trying to do here, is to create a variable of a child snapshot (achievements), and access child snapshot data from it. The achievements[0] will simply return the very first value. However, this doesn't seem to work. How should I approach this?

I am currently doing this inside a snapshot already (of 'observeType:.ChildAdded')

My Firebase DB looks like this:

Achievements{

randomId1 {
somevalue : somevalue
}

randomId2 {
somevalue2 : somevalue2
}
}

Updated code:

 func loadData() {

        ref.child("Players").observe(FIRDataEventType.childAdded) { (snapshot:FIRDataSnapshot) in

            if let value = snapshot.value as? [String:AnyObject], let username = value["Username"] as? String {

            }

            if let value = snapshot.value as? [String:AnyObject], let ranking = value["Rank"] as? String {

            }

            if let childSnapshot = snapshot.childSnapshot(forPath: "Achievements").children.allObjects as? [FIRDataSnapshot] {

                if childSnapshot.count != 0 {

                    if let achievement1 = childSnapshot[0].value!["Rookie"] as? String {
                        print(achievement1)
                    }

                }
            }

        }

    }

JSON Tree:

Players {

PlayerID {

Username

Rank

Achievements {

Rookie: yes

}


}
AL.
  • 36,815
  • 10
  • 142
  • 281
askaale
  • 1,199
  • 1
  • 23
  • 50
  • Give your full function. And your Actual JSON tree, its hard to decipher from what you gave. – Dravidian Sep 21 '16 at 18:12
  • I have now provided you with the full code and JSON tree. I really can't seem to find a way to grab the childsnapshot data, the code above returns an error. I have also tried to define it as [String:AnyObject] - but this only results in a nil. – askaale Sep 21 '16 at 18:27
  • And to be clear, I simply just want to return the 'yes' from the 'Rookie'-value. – askaale Sep 21 '16 at 18:36

1 Answers1

5

Try :-

 if let childSnapshot = snapshot.childSnapshot(forPath: "Achievements") as? FIRDataSnapshot{

                if let achievementDictionary = childSnapshot.value as? [String:AnyObject] , achievementDictionary.count > 0{

                    if let achieveMedal = achievementDictionary["Rookie"] as? String {

                        print(achieveMedal)

             }        
       }
    }
Dravidian
  • 9,945
  • 3
  • 34
  • 74
  • This is also something that I tried, and it leaves me with an error message like this: Type 'Any' has no subscript members. – askaale Sep 21 '16 at 18:44
  • Try replacing `[FIRDataSnapshot]` to `[String:AnyObject]` – Dravidian Sep 21 '16 at 18:50
  • Error: 'ambiguous reference to member 'subscript'. – askaale Sep 21 '16 at 18:53
  • By the way, I just came to realize that I need to change the code further - because I want multiple achievements to be retrieved. In other words, under 'Achievements' I need to have multiple childByAutoID, that again contains a value that I want to retrieve. How would I do this? 'if let achievementDictionary = childSnapshot.childAdded as? [String:AnyObject]' does obviously not work. Any deas? – askaale Sep 21 '16 at 20:30
  • Avoid stretching new question in comments, SO has no restriction on how many Q you post in a day. Post a different Q. :) – Dravidian Sep 21 '16 at 20:31
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/123893/discussion-between-askaale-and-dravidian). – askaale Sep 21 '16 at 20:33