Last active
April 26, 2016 13:32
-
-
Save jk/59936950ae3a2612775f2f6a49fef53d to your computer and use it in GitHub Desktop.
ReflectedStringConvertible
This file contains hidden or 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
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