Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve a value from dictionary in Swift 3

I have this function that fetch users from FireBase and convert them in Dictionary:

 let leaderBoardDB = FIRDatabase.database().reference().child("scores1").queryOrderedByValue().queryLimited(toLast: 5)
        leaderBoardDB.observe( .value, with: { (snapshot) in
            print("scores scores", snapshot)

            if let dictionary = snapshot.value as? [String: Any] {
                for playa in dictionary {
                    let player = Player()

                    print("plaaaaayyyyyaaaaa", playa)
                    print("plaaaaayyyyyaaaaa key", playa.key)
                    print("plaaaaayyyyyaaaaa.value", playa.value)
                        player.id = playa.key
                    print(playa.key["name"])

                }
            }   
          }, withCancel: nil)
    }

and I get this result:

plaaaaayyyyyaaaaa ("inovoID", { name = Tatiana; points = 6; }) plaaaaayyyyyaaaaa key inovoID plaaaaayyyyyaaaaa.value { name = Tatiana; points = 6; } aaaa i id Optional("inovoID")

the problem is that i can't obtain the name and the points of the user. when i try it with:

print(playa.key["name"])

it gaves me this error:

Cannot subscript a value of type 'String' with an index of type 'String'

can anyone help me with this, please?

like image 656
John Smith Optional Avatar asked Dec 09 '16 18:12

John Smith Optional


People also ask

How do you check if a value is in a dictionary Swift?

Swift – Check if Specific Key is Present in Dictionary To check if a specific key is present in a Swift dictionary, check if the corresponding value is nil or not. If myDictionary[key] != nil returns true, the key is present in this dictionary, else the key is not there.

Is dictionary value type in Swift?

In Swift, Array, String, and Dictionary are all value types. They behave much like a simple int value in C, acting as a unique instance of that data. You don't need to do anything special — such as making an explicit copy — to prevent other code from modifying that data behind your back.

Can a dictionary key have multiple values Swift?

Possible duplicate of How to add multiple values for one key in a dictionary using swift. The idea in a dictionary is that the key is a 'primary key'. That is, a key can only return one value. Be it an object or array.


2 Answers

Since your JSON is

"inovoID" : { "name" : "Tatiana", "points" : 6 }
  • playa.key is "inovoID"
  • playa.value is { "name" : "Tatiana", "points" : 6 }

The key is String and cannot be subscripted. That's what the error says.

You need to subscribe the value and safely cast the type to help the compiler.

if let person = playa.value as? [String:Any] {
   print(person["name"] as! String)
}
like image 64
vadian Avatar answered Nov 09 '22 23:11

vadian


I think you're looking for:

player.name = dictionary["name"] as? String

You don't need to iterate through the dictionary to access it's values. If you're looking for the value of a key, just get it.

like image 40
GetSwifty Avatar answered Nov 10 '22 01:11

GetSwifty