Skip to content

Instantly share code, notes, and snippets.

@romainmenke
Created May 29, 2016 13:01
Show Gist options
  • Select an option

  • Save romainmenke/1ad1fa2caebd0d4b8bec2791e6e3ec96 to your computer and use it in GitHub Desktop.

Select an option

Save romainmenke/1ad1fa2caebd0d4b8bec2791e6e3ec96 to your computer and use it in GitHub Desktop.
import Foundation
protocol MirrorDescribable : CustomStringConvertible {}
extension MirrorDescribable {
var description : String {
return mirrorDescription
}
var mirrorDescription : String {
get {
var descr : String = "\n\(self.dynamicType)\n"
let mirror = Mirror(reflecting: self)
for child in mirror.children {
if let label = child.label {
descr += label + " : "
}
if let value = child.value as? String {
descr += value
} else if let value = child.value as? MirrorDescribable {
descr += "\n\t" + value.mirrorDescription.stringByReplacingOccurrencesOfString("\n", withString: "\n\t")
} else if let value = child.value as? OptionalMirrorDescribable {
descr += "\n\t" + value.mirrorDescription.stringByReplacingOccurrencesOfString("\n", withString: "\n\t")
} else if let value = child.value as? CustomStringConvertible {
descr += value.description
}
descr += "\n"
}
descr += "\n"
return descr
}
}
}
extension Optional {
private var value : Any? {
return self
}
}
private protocol OptionalMirrorDescribable {
var value : Any? { get }
}
extension Optional : OptionalMirrorDescribable {}
extension OptionalMirrorDescribable {
var description : String {
return mirrorDescription
}
var mirrorDescription : String {
get {
if let sure = self.value as? MirrorDescribable {
return sure.mirrorDescription
} else if let sure = self.value as? CustomStringConvertible {
return sure.description
} else if let sure = self.value as? String {
return sure
}
return ""
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment