Check if the object is an instance of the meta class in Swift

I have an array of objects of various types and an array of types. For each object, I would like to iterate over an array of types and see if the object is such a type. Something like that:

class Parent {}
class ChildA: Parent {}
class ChildB: Parent {}
class GrandChildA: ChildA {}

var objects: [Any] = ["foo", ChildA(), ChildA(), ChildB(), GrandChildA()]
var classes = [Parent, ChildA, ChildB] // This line doesn't compile!!

for obj in objects {
    for cls in classes {
        if obj is cls {
            NSLog("obj matches type!")
        }
    }
}

This does not work because you cannot store classes in an array. As far as I understand, you can store class types, such as ChildA.self:

ChildA().dynamicType == ChildA.self // true

But this does not apply to subclasses:

ChildA().dynamicType == Parent.self // false

Obviously, the operator issolves the case of a subclass:

ChildA() is Parent // true

But if I want to use is, I do not know how to store class types in an array.

Can I accomplish what I want to somehow use Swift and some voodoo reflection?

Sorry if the headline is misleading - I don't understand this well enough to form the correct question.

+4
1

, . , ?

, - :

protocol Parentable {
    var title : String { get set }
}

class ChildA : Parentable{
    var title: String
    init() {
        self.title = "ChildA"
    }
    init(title: String){
       self.title = title
    }
}
class ChildB: Parentable {
    var title: String
    init(){
        self.title = "ChildB"
    }
}
class GrandChildA: ChildA {
    override init() {
        super.init(title: "GrandChild")
    }
}

var objects: [Parentable] = [ChildA(), ChildA(), ChildB(), GrandChildA()]

for obj in objects {
    switch obj.title {
    case "ChildA":
        print("Object is of type \(obj.title)")
    case "ChildB":
        print("Object is of type \(obj.title)")
    case "GrandChild":
        print("Object is of type \(obj.title)")
    default :
        print("Object is of type \(obj.title)")
    }
}

, , , :

class Parent {
    var title: String

    init() {
        self.title = "Parent"
    }
    init(title: String) {
        self.title = title
    }
}
class ChildA: Parent {
    override init() {
        super.init(title: "ChildA")
    }
    override init(title: String) {
        super.init(title: title)
    }
}
class ChildB: Parent {
    override init() {
        super.init(title: "ChildB")
    }
}
class GrandChildA: ChildA {
    override init() {
        super.init(title: "GrandChild")
    }
}

    var objects: [Parent] = [ChildA(), ChildA(), ChildB(), GrandChildA()]

    for obj in objects {
        switch obj.title {
        case "ChildA":
            print("Object is of type \(obj.title)")
        case "ChildB":
            print("Object is of type \(obj.title)")
        case "GrandChild":
            print("Object is of type \(obj.title)")
        default :
            print("Object is of type \(obj.title)")
    }
}
-2

Source: https://habr.com/ru/post/1652272/


All Articles