Skip to content

Instantly share code, notes, and snippets.

@mattcomi
Last active April 21, 2016 09:03
Show Gist options
  • Save mattcomi/33e6817068a9fffcd8d1ef3ddf0df16e to your computer and use it in GitHub Desktop.
Save mattcomi/33e6817068a9fffcd8d1ef3ddf0df16e to your computer and use it in GitHub Desktop.
public protocol ReflectedStringConvertible : CustomStringConvertible { }
extension ReflectedStringConvertible {
public var description: String {
let mirror = Mirror(reflecting: self)
var str = "\(mirror.subjectType)("
var first = true
for (label, value) in mirror.children {
if let label = label {
if first {
first = false
} else {
str += ", "
}
str += label
str += ": "
str += "\(value)"
}
}
str += ")"
return str
}
}
@jamesbebbington
Copy link

I've been using something similar and it's been very helpful. You could simplify your implementation like so:

mirror.children.map { "\($0): \($1)" }.joinWithSeparator(", ")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment