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

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


        
相关标签:
16条回答
  • 2020-12-07 10:23

    Details

    • Xcode 10.2.1 (10E1001), Swift 5

    Solution

    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
            }
        }
    }
    

    Usage

    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")
    

    Results

    0 讨论(0)
  • 2020-12-07 10:23

    swift 5, xcode 10.3:

    po print(<your Plist container>)
    
    0 讨论(0)
  • 2020-12-07 10:23
    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
    
    0 讨论(0)
  • 2020-12-07 10:23

    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.

    0 讨论(0)
提交回复
热议问题