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]
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
But this does not apply to subclasses:
ChildA().dynamicType == Parent.self
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.