How to get class name in Swift

Swift Class

Posted on 20 May 2023 . 1 min read


In Swift, you can get the name of a class by using the String(describing:) function along with the type(of:) function. Here's an example:


class MyClass {
    // Class implementation
}

let className = String(describing: type(of: MyClass.self))
print(className) // Output: "MyClass"


This will give you the name of the class as a string, which can be useful for various purposes, such as debugging or logging.

Don’t hesitate to contact me if you have any questions or queries. Follow me on twitter @gurjitpt for any updates.

Thanks!


Share this article



Written By

Generic placeholder image

Gurjit Singh

I’m Computer Science graduate and an iOS Engineer who writes about Swift and iOS development. Follow me for more updates:


Discover articles by topics

SwiftUI Class Struct Networking XCode NSCache Enum Optionals Property Observers Closures Guard Reviews StoreKit App Store Algorithms Testing Operators Protocol Extensions Weak Unowned SwiftData WWDC23 GCD API Admob SwiftLint Lottie Foreach Objective-C UIKit NavigationSplitView

Related Articles


Connection failed: Access denied for user 'techwyhu'@'server82.web-hosting.com' (using password: YES)