Get as string from dump(:) in Swift

Updated
Feb 16, 2020 5:26 PM
Created
Feb 16, 2020 5:24 PM
Tags
SwiftMemo
Keywords
Date

In Swift, dump function is very helpful during debugging.

However, the default behavior of dump prints to console directly with stdout.

In order to get as string from text that dump printed, use text-stream argument dump has.

public func dumpString<T>(_ object: T) -> String {
  var dumpString = ""
  Swift.dump(object, to: &dumpString, name: "", indent: 2, maxDepth: .max, maxItems: .max)
  return dumpString
}