Get the name (string) of a generic type in Swift

You can return any types’ name by using string interpolation:

class MyClass<T> {
    func genericName() -> String {
        return "\(T.self)"
    }
}

You can try it in a playground and it works as expected:

var someClass = MyClass<String>()
someClass.genericName() // Returns "Swift.String"

Leave a Comment

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