Is there a way to pretty print Swift dictionaries to the console?

前端 未结 16 969
北海茫月
北海茫月 2020-12-07 09:33
NSDictionary *dictionary = @{@\"A\" : @\"alfa\",
                             @\"B\" : @\"bravo\",
                             @\"C\" : @\"charlie\",
                       


        
16条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-07 10:22

    Adjusted based on my other answer here.

    PrettyPrint JSON solution using LLDB alias

    No code needed

    • To get a nice json formatting (indentations, newlines, etc) you can define an lldb alias by running this command in your lldb terminal (source):
    command regex pjson 's/(.+)/expr print(NSString(string: String(data: try! JSONSerialization.data(withJSONObject: %1, options: .prettyPrinted), encoding: .utf8)!))/'
    
    • You probably don't want to re-define the alias everytime you open XCode, so run the following command to append the alias definition to ~/.lldbinit:
    echo "command regex pjson 's/(.+)/expr print(NSString(string: String(data: try! JSONSerialization.data(withJSONObject: %1, options: .prettyPrinted), encoding: .utf8)!))/'" >> ~/.lldbinit
    
    • This will create the pjson alias which you can use in your lldb terminal in XCode:
    pjson object
    

    Comparing the outputs for the following Swift object:

    // Using Any? to demo optional & arbitrary Type
    let dictionary: Any? = [
        "embedded": [
            "JustForTheSakeOfTheDemo": 42
        ],
        "A" : "alfa",
        "B" : "bravo",
        "C" : "charlie",
        "D" : "delta",
        "E" : "echo",
        "F" : "foxtrot"
    ]
    

    ✅ Output of pjson dictionary

    {
      "F" : "foxtrot",
      "D" : "delta",
      "embedded" : {
        "JustForTheSakeOfTheDemo" : 42
      },
      "E" : "echo",
      "A" : "alfa",
      "C" : "charlie",
      "B" : "bravo"
    }
    

    ❌ Output of 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 of p (dictionary as! NSDictionary)

    (NSDictionary) $R18 = 0x0000000281e89710 {
      ObjectiveC.NSObject = {
        base__SwiftNativeNSDictionaryBase@0 = {
          baseNSDictionary@0 = {
            NSObject = {
              isa = Swift._SwiftDeferredNSDictionary with unmangled suffix "$"
            }
          }
        }
      }
    }
    

    ❌ Output of po dictionary

    ▿ Optional
      ▿ 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 of po print(dictionary)

    Optional(["F": "foxtrot", "D": "delta", "embedded": ["JustForTheSakeOfTheDemo": 42], "E": "echo", "A": "alfa", "C": "charlie", "B": "bravo"])
    

提交回复
热议问题