Avatar ·

How to get the class name of an object as a string in Swift?

Hello everyone! I'm new to Swift and trying to understand how to get the class name of an object as a string. In Objective-C I used `NSStringFromClass([self class])`, but this doesn't work in Swift. I need to log object types at runtime for debugging. For example, I have a protocol `Animal` and several structs that implement it: `Dog`, `Cat`, and `Bird`. In the function `printAnimalType(animal: Animal)`, I want to print the name of the specific type that was passed, not just "Animal". Here's an example of my code: ```swift protocol Animal { func makeSound() } struct Dog: Animal { func makeSound() { print("Woof!") } } struct Cat: Animal { func makeSound() { print("Meow!") } } func printAnimalType(animal: Animal) { // How to get the string "Dog" or "Cat" here }