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

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


        
16条回答
  •  悲哀的现实
    2020-12-07 10:14

    How about:

    import Foundation
    
    extension Dictionary {
        var myDesc: String {
            get {
                var v = ""
                for (key, value) in self {
                    v += ("\(key) = \(value)\n")
                }
                return v
            }
        }
    }
    
    
    // Then, later, for any dictionary:
    print(dictionary.myDesc)
    

提交回复
热议问题