What is the Swift equivalent of -[NSObject description]?

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’.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)