Skip to content

Instantly share code, notes, and snippets.

@jk
Last active April 26, 2016 13:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jk/59936950ae3a2612775f2f6a49fef53d to your computer and use it in GitHub Desktop.
Save jk/59936950ae3a2612775f2f6a49fef53d to your computer and use it in GitHub Desktop.
ReflectedStringConvertible
public protocol ReflectedStringConvertible: CustomStringConvertible { }
extension ReflectedStringConvertible {
public var description: String {
let mirror = Mirror(reflecting: self)
let classType = "\(mirror.subjectType)("
return classType + mirror.children.flatMap { (label, value) in
if let label = label {
let subMirror = Mirror(reflecting: value)
return "\(label): \(subMirror.subjectType)(\(value))"
}
return nil
}.joinWithSeparator(", ") + ")"
}
}
class Person: ReflectedStringConvertible {
var firstname: String
var lastname: String
var age: Int
init(firstname: String, lastname: String, age: Int) {
self.firstname = firstname
self.lastname = lastname
self.age = age
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment