Created
May 29, 2016 13:01
-
-
Save romainmenke/1ad1fa2caebd0d4b8bec2791e6e3ec96 to your computer and use it in GitHub Desktop.
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
| 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