Skip to content

Instantly share code, notes, and snippets.

@harshvishu
Created April 20, 2025 11:04
Show Gist options
  • Save harshvishu/ced347417ba5a5e8a9fc80c09c1931dd to your computer and use it in GitHub Desktop.
Save harshvishu/ced347417ba5a5e8a9fc80c09c1931dd to your computer and use it in GitHub Desktop.
Defines a protocol StringRepresentable that inherits from CustomStringConvertible, and provides a default implementation of description using reflection. It generates a string listing all the property names and values of any conforming type.
public protocol StringRepresentable: CustomStringConvertible { }
extension StringRepresentable {
public var description: String {
Mirror(reflecting: self).children.reduce("") { $0 + ($1.label ?? "").appending(":\($1.value as Any)\n") }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment