Avatar ·

How to print the type or class of a variable in Swift?

Hello everyone! I just started learning Swift and ran into a problem. In other languages, like Python or C#, I can easily find out the type of a variable at runtime using built-in functions. But in Swift, I can't find an equivalent.\n\nFor example, I have this code:\n\n```swift\nvar myVariable = "Hello"\nvar anotherVar = 42\nvar optionalVar: Any? = nil\n```\n\nI want to print to the console what type each of these variables has. In Python, I would write `print(type(myVariable))`, and in C# — `Console.WriteLine(myVariable.GetType())`. How do I do the same in Swift?\n\nI need exactly dynamic type determination, so the code determines the type of the variable itself, rather than me specifying it manually. This is especially important for working with protocols and generics, where the type may be unknown at compile time.