Last active
June 4, 2017 11:43
-
-
Save asaday/2b86f6ea1a614014e905244ebd142432 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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