NSClassFromString in swift usage Cannot find type 'hackType' in scope

My goal is to take string (class name) create a class from it using the NSStrngFromClass and check if object type is of type I got from NSStrngFromClass.

Something like that:

public extension UIView {
    func someMethod() -> Bool {

        let objType = type(of: self)
        let hackType = NSClassFromString(className: "")
        if objType is hackType {
//Error: Cannot find type 'hackType' in scope
            return true;
  1. I am not sure I understand the error
  2. how I can achieve what I want (assuming that classes with generics should return true)?


is cannot be used for variables of class type, only hard-coded types. I ran into this when solving a different problem a couple years ago.

1 Like

Looking more closely at your code, I don’t think is is what you want here anyways? I think == should work fine.

On the off chance you do want is, since only Objective-C types are involved here, you could probably use the isKindOfClass: method (not 100% sure what that’s called in Swift).

I forgot to mention that it should support generics. I mean same Classe with different generics will return false on == .
isKindOfClasss is not available in swift AFAIK (I could possible create an objective c method and call it from swift)