To implement this on a Swift type you must implement the CustomStringConvertible
protocol and then also implement a string property called description
.
For example:
class MyClass: CustomStringConvertible {
let foo = 42
var description: String {
return "<\(type(of: self)): foo = \(foo)>"
}
}
print(MyClass()) // prints: <MyClass: foo = 42>
Note: type(of: self)
gets the type of the current instances instead of explicitly writing ‘MyClass’.