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

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


        
16条回答
  •  情歌与酒
    2020-12-07 10:00

    The methodology of converting the Swift Dictionary to json and back is the neatest. I use Facebook's chisel which has a pjson command to print a Swift dictionary. Eg:

    (lldb) pjson dict as NSDictionary
    

    This should pretty-print the dictionary. This is a much cleaner way to do what has already been suggested. P.S. For now, you'll have to cast dict as NSDictionary because Objective-C runtime doesn't understand Swift dictionaries. I have already raised a PR on chisel to get rid of that restriction.

    UPDATE: My PR got accepted. Now you can use psjson command instead of pjson mentioned above.

提交回复
热议问题