Skip to content

Instantly share code, notes, and snippets.

@iosdevzone
Created October 23, 2017 22:27
Show Gist options
  • Select an option

  • Save iosdevzone/8e7bcb904f604eeba2c6a2b5fd39d322 to your computer and use it in GitHub Desktop.

Select an option

Save iosdevzone/8e7bcb904f604eeba2c6a2b5fd39d322 to your computer and use it in GitHub Desktop.
Swift: debugDescription excluding some fields
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