Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode print keys and values of Dictionary

Tags:

xcode7

swift2

Print description in Xcode 7 is giving memory addresses similar to below. Tried all the options, but getting output like this.

▿ 3 elements
  ▿ [0] : 2 elements
    - .0 : Output
    ▿ .1 : 3 elements
      ▿ [0] : 2 elements
        - .0 : type
        - .1 : Output2 { ... }
      ▿ [1] : 2 elements
        - .0 : version
        - .1 : 1.0
      ▿ [2] : 2 elements
        - .0 : content
        ▿ .1 : 2 elements

How can I instead print the exact values of a dictionary?

like image 336
Coder Avatar asked Oct 04 '15 18:10

Coder


People also ask

How do I print a key in Swift?

To print all the keys of a dictionary, we can iterate over the keys returned by Dictionary. keys or iterate through each (key, value) pair of the dictionary and then access the key alone.

How do you check if a key exists in a dictionary Swift?

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.


2 Answers

Try with CFShow:

Prints a description of a Core Foundation object to stderr.

 po CFShow(dict)
like image 69
Luca Davanzo Avatar answered Sep 28 '22 19:09

Luca Davanzo


I created this test dictionary:

let dict:Dictionary = ["key1": "value1", "key2": 42, "keyForColor": UIColor.redColor()]

Then I used po dict and get the result you described:

po dict
▿ 3 elements
  ▿ [0] : 2 elements
    - .0 : "key1"
    - .1 : value1
  ▿ [1] : 2 elements
    - .0 : "keyForColor"
  ▿ [2] : 2 elements
    - .0 : "key2"

When you use po dict.description, you get this:

po dict.description
"[\"key1\": value1, \"keyForColor\": UIDeviceRGBColorSpace 1 0 0 1, \"key2\": 42]"
like image 36
joern Avatar answered Sep 28 '22 20:09

joern