Created
April 20, 2025 11:04
-
-
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.
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
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