Disesuaikan berdasarkan jawaban saya yang lain di sini .
Solusi PrettyPrint JSON menggunakan alias LLDB
✨ Tidak perlu kode ✨
- Untuk mendapatkan format json yang bagus (lekukan, baris baru, dll) Anda dapat mendefinisikan alias lldb dengan menjalankan perintah ini di terminal lldb Anda ( sumber ):
command regex pjson 's/(.+)/expr print(NSString(string: String(data: try! JSONSerialization.data(withJSONObject: %1, options: .prettyPrinted), encoding: .utf8)!))/'
- Anda mungkin tidak ingin mendefinisikan ulang alias setiap kali Anda membuka XCode, jadi jalankan perintah berikut untuk menambahkan definisi alias ke
~/.lldbinit
:
echo "command regex pjson 's/(.+)/expr print(NSString(string: String(data: try! JSONSerialization.data(withJSONObject: %1, options: .prettyPrinted), encoding: .utf8)!))/'" >> ~/.lldbinit
- Ini akan membuat
pjson
alias yang dapat Anda gunakan di terminal lldb Anda di XCode:
pjson object
Membandingkan keluaran untuk objek Swift berikut:
let dictionary: Any? = [
"embedded": [
"JustForTheSakeOfTheDemo": 42
],
"A" : "alfa",
"B" : "bravo",
"C" : "charlie",
"D" : "delta",
"E" : "echo",
"F" : "foxtrot"
]
✅ Output dari pjson dictionary
{
"F" : "foxtrot",
"D" : "delta",
"embedded" : {
"JustForTheSakeOfTheDemo" : 42
},
"E" : "echo",
"A" : "alfa",
"C" : "charlie",
"B" : "bravo"
}
❌ Output dari p dictionary
(Any?) $R0 = 7 key/value pairs {
[0] = {
key = "F"
value = "foxtrot"
}
[1] = {
key = "D"
value = "delta"
}
[2] = {
key = "embedded"
value = 1 key/value pair {
[0] = (key = "JustForTheSakeOfTheDemo", value = 42)
}
}
[3] = {
key = "E"
value = "echo"
}
[4] = {
key = "A"
value = "alfa"
}
[5] = {
key = "C"
value = "charlie"
}
[6] = {
key = "B"
value = "bravo"
}
}
❌ Output dari p (dictionary as! NSDictionary)
(NSDictionary) $R18 = 0x0000000281e89710 {
ObjectiveC.NSObject = {
base__SwiftNativeNSDictionaryBase@0 = {
baseNSDictionary@0 = {
NSObject = {
isa = Swift._SwiftDeferredNSDictionary<Swift.String, Any> with unmangled suffix "$"
}
}
}
}
}
❌ Output dari po dictionary
▿ Optional<Any>
▿ some : 7 elements
▿ 0 : 2 elements
- key : "F"
- value : "foxtrot"
▿ 1 : 2 elements
- key : "D"
- value : "delta"
▿ 2 : 2 elements
- key : "embedded"
▿ value : 1 element
▿ 0 : 2 elements
- key : "JustForTheSakeOfTheDemo"
- value : 42
▿ 3 : 2 elements
- key : "E"
- value : "echo"
▿ 4 : 2 elements
- key : "A"
- value : "alfa"
▿ 5 : 2 elements
- key : "C"
- value : "charlie"
▿ 6 : 2 elements
- key : "B"
- value : "bravo"
❌ Output dari po print(dictionary)
Optional(["F": "foxtrot", "D": "delta", "embedded": ["JustForTheSakeOfTheDemo": 42], "E": "echo", "A": "alfa", "C": "charlie", "B": "bravo"])
dump
, misalnya, jika tujuannya adalah untuk memeriksa kamus. stackoverflow.com/documentation/swift/3966/logging-in-swift/…