Skip to content

Instantly share code, notes, and snippets.

@lalkrishna
Created October 17, 2019 06:14
Show Gist options
  • Save lalkrishna/8764263eecd72ac9dcdc7246fc313958 to your computer and use it in GitHub Desktop.
Save lalkrishna/8764263eecd72ac9dcdc7246fc313958 to your computer and use it in GitHub Desktop.
Extension for CustomStringConvertible to print All properties of a class
extension CustomStringConvertible {
var description: String {
var description: String = "\(type(of: self))("
let selfMirror = Mirror(reflecting: self)
if let superclassMirror = selfMirror.superclassMirror {
for child in superclassMirror.children {
if let propertyName = child.label {
description += "Super.\(propertyName): \(child.value), "
}
}
}
for child in selfMirror.children {
if let propertyName = child.label {
description += "\(propertyName): \(child.value), "
}
}
description += "<\(Unmanaged.passUnretained(self as AnyObject).toOpaque())>)"
return description
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment