Created
October 23, 2017 22:27
-
-
Save iosdevzone/8e7bcb904f604eeba2c6a2b5fd39d322 to your computer and use it in GitHub Desktop.
Swift: debugDescription excluding some fields
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
| extension CustomDebugStringConvertible { | |
| public func debugDescription(excluding fields: [String]) -> String { | |
| let excluded = Set<String>(fields) | |
| let m = Mirror(reflecting: self) | |
| let arguments = m.children | |
| .filter { child in | |
| return (child.label == nil) || !excluded.contains(child.label!) | |
| } | |
| .map { c -> String in | |
| let value = (type(of: c.value) == String.self) ? "\"\(c.value)\"" : "\(c.value)" | |
| if let label = c.label { | |
| return "\(label): \(value)" | |
| } | |
| else { | |
| return "\(value)" | |
| } | |
| } | |
| .joined(separator: ", ") | |
| let debugDescription = "\(m.subjectType)(\(arguments))" | |
| return debugDescription | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment