I need to determine the dynamic type of my object in a method that is implemented in a super class. The super class is called BaseClient and DisplayClient inherits from it.
I only need the class name, not the package name. This is what I tried:
print("1", String(describing: type(of: self))) // DisplayClient
print("2", type(of: self)) // DisplayClient
print("3", type(of: self).description()) // package.DisplayClient
print("4", "\(type(of: self))") // DisplayClient
Why does
type(of: self).description()
return package.DisplayClient while the others only return the class name? I wonder what is called internally when I use String(describing: type(of: self)). I would assume this does exactly what I do (call describing()).
Where can I find more info on how the strings get generated internally?
The docs say:
Use this initializer to convert an instance of any type to its preferred representation as a String instance. The initializer creates the string representation of instance in one of the following ways, depending on its protocol conformance:
- If instance conforms to the TextOutputStreamable protocol, the result is obtained by calling instance.write(to: s) on an empty string s.
- If instance conforms to the CustomStringConvertible protocol, the result is instance.description.
- If instance conforms to the CustomDebugStringConvertible protocol, the result is instance.debugDescription.
- An unspecified result is supplied automatically by the Swift standard library.
But type(of: self) does not even have a description attribute. It only has a description() method. Is this some special case that is handled differently by the compiler?