NSDictionary *dictionary = @{@"A" : @"alfa", @"B" : @"bravo", @"C" : @"charlie", @"D" : @"delta", @"E" : @"echo", @"F" : @"foxtrot"}; NSLog(@"%@", dictionary.description);
prints out the following on the console:
{ A = alfa; B = bravo; C = charlie; D = delta; E = echo; F = foxtrot; }
let dictionary: [String : String] = ["A" : "alfa", "B" : "bravo", "C" : "charlie", "D" : "delta", "E" : "echo", "F" : "foxtrot"]; print(dictionary)
prints out the following on the console:
["B": "bravo", "A": "alfa", "F": "foxtrot", "C": "charlie", "D": "delta", "E": "echo"]
Is there a way in Swift to get it to pretty print dictionaries where each key-value pair occupies a new line?
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.
If you assign a created dictionary to a variable, then it is always mutable which means you can change it by adding, removing, or changing its items. But if you assign a dictionary to a constant, then that dictionary is immutable, and its size and contents cannot be changed.
Sets are unordered collections of unique values. Dictionaries are unordered collections of key-value associations. Arrays, sets, and dictionaries in Swift are always clear about the types of values and keys that they can store. This means that you can't insert a value of the wrong type into a collection by mistake.
po solution
For those of you want to see Dictionary as JSON with out escape sequence in console, here is a simple way to do that
(lldb)p print(String(data: try! JSONSerialization.data(withJSONObject: object, options: .prettyPrinted), encoding: .utf8 )!)
Update
Check this answer too Answer
Casting a dictionary to 'AnyObject' was the simplest solution for me:
let dictionary = ["a":"b", "c":"d", "e":"f"] print("This is the console output: \(dictionary as AnyObject)")
This is easier to read for me than the dump option, but note it won't give you the total number of key-values.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With