Skip to content

Instantly share code, notes, and snippets.

@asaday
Last active June 4, 2017 11:43
Show Gist options
  • Save asaday/2b86f6ea1a614014e905244ebd142432 to your computer and use it in GitHub Desktop.
Save asaday/2b86f6ea1a614014e905244ebd142432 to your computer and use it in GitHub Desktop.
func refrectedString(_ iobj: Any?, indent: String = "") -> String {
guard let obj = iobj else { return "null" }
let m = Mirror(reflecting: obj)
if m.displayStyle == nil {
if let s = obj as? String { return "\"\(s)\"" }
return "\(obj)"
}
if m.displayStyle == .optional {
return refrectedString(m.children.first?.value)
}
if m.displayStyle == .struct {
if let s = obj as? Date { return "\(s)" }
}
let ci = indent + " "
let ri = "\n" + ci
let ar: [String] = m.children.flatMap {
guard let name = $0.label else { return nil }
if Mirror(reflecting: $0.value).displayStyle == .collection, let car = $0.value as? [Any] {
return name + ": [" + car.map { refrectedString($0, indent: ci) }.joined(separator: ", ") + "]"
}
return name + ": " + refrectedString($0.value, indent: ci)
}
return "{" + ri + ar.joined(separator: "," + ri) + "\n" + indent + "}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment