NSDictionary *dictionary = @{@\"A\" : @\"alfa\",
@\"B\" : @\"bravo\",
@\"C\" : @\"charlie\",
extension Dictionary {
func format(options: JSONSerialization.WritingOptions) -> Any? {
do {
let jsonData = try JSONSerialization.data(withJSONObject: self, options: options)
return try JSONSerialization.jsonObject(with: jsonData, options: [.allowFragments])
} catch {
print(error.localizedDescription)
return nil
}
}
}
let dictionary: [String : Any] = [
"id": 0,
"bool": true,
"int_array": [1,3,5],
"dict_array": [
["id": 1, "text": "text1"],
["id": 1, "text": "text2"]
]
]
print("Regualr print:\n\(dictionary)\n")
guard let formatedDictionary = dictionary.format(options: [.prettyPrinted, .sortedKeys]) else { return }
print("Pretty printed:\n\(formatedDictionary)\n")
swift 5, xcode 10.3:
po print(<your Plist container>)
extension String {
var conslePrintString: String {
guard let data = "\""
.appending(
replacingOccurrences(of: "\\u", with: "\\U")
.replacingOccurrences(of: "\"", with: "\\\"")
)
.appending("\"")
.data(using: .utf8) else {
return self
}
guard let propertyList = try? PropertyListSerialization.propertyList(from: data,
options: [],
format: nil) else {
return self
}
guard let string = propertyList as? String else {
return self
}
return string.replacingOccurrences(of: "\\r\\n", with: "\n")
}
}
let code in extension String and it works fine
let string = "\(jsonDictionary)".conslePrintString
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.