0

I'm still trying to access all the nodes but when I'm writing this code I'm getting the error written below. I would like to print "yes" for golf if this sport is in my database.

Everything works before the line let dict = snap.value as! [String: any]. ie I can write the different snaps, but then it crashes.

All the elements in my database are strings, even the coordinates. You can find a picture of my database with the link below.

@IBAction func testbutton(_ sender: UIButton) {
    Database.database().reference().child("users").observe(.childAdded) { (snapshot) in
       for child in snapshot.children {
        print(child)
            let snap = child as! DataSnapshot
            print(snap)
            let dict = snap.value as! [String: Any]
            let msg = dict["golf"] as! String
            print(msg)

Error:

___________________________________________________
Could not cast value of type 'NSTaggedPointerString' 
(0x7fff87a91958) to 'NSDictionary' (0x7fff87a91c78).
2020-07-21 13:18:24.131870+0200 Joiny[43447:2152693] 
Could not cast value of type 'NSTaggedPointerString' 
(0x7fff87a91958) to 'NSDictionary' (0x7fff87a91c78).
_______________________________________________________

Picture of my database -> 1

1 Answer 1

2

You need

Database.database().reference().child("users").observe(.childAdded) { (snapshot) in  
  let dict = snapshot.value as! [String: Any]
  let res = dict["golf"] as! [String: Any]
  let msg = res["golf"] as! String
  print(msg)
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank for your reply, but I have another error message now: Fatal error: Unexpectedly found nil while unwrapping an Optional value: file /Users/guillaumebellanger/Desktop/Joiny post map copie 3/Joiny/RubriqueViewController.swift, line 201 2020-07-21 15:03:45.694101+0200 Joiny[44290:2207028]

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.