Check if an object is an instance of class meta type in Swift -


this question has answer here:

i have array of objects of various types , array of types. each object, i'd iterate on array of types , see if object type. this:

class parent {} class childa: parent {} class childb: parent {} class grandchilda: childa {}  var objects: [any] = ["foo", childa(), childa(), childb(), grandchilda()] var classes = [parent, childa, childb] // line doesn't compile!!  obj in objects {     cls in classes {         if obj cls {             nslog("obj matches type!")         }     } } 

this doesn't work because can't store classes in array. understand, can store class types such childa.self:

childa().dynamictype == childa.self // true 

but doesn't handle subclasses:

childa().dynamictype == parent.self // false 

obviously is operator solves subclass case:

childa() parent // true 

but if want use is, don't know how store class types in array.

can accomplish want somehow using swift , reflection voodoo?

sorry if title misleading -- don't understand enough form proper question.

by adding title each class can switch on them. also, have thought of using protocol instead of superclass?

maybe this:

protocol parentable {     var title : string { 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()]  obj in objects {     switch obj.title {     case "childa":         print("object of type \(obj.title)")     case "childb":         print("object of type \(obj.title)")     case "grandchild":         print("object of type \(obj.title)")     default :         print("object of type \(obj.title)")     } } 

you superclass if need objects passed reference:

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()]      obj in objects {         switch obj.title {         case "childa":             print("object of type \(obj.title)")         case "childb":             print("object of type \(obj.title)")         case "grandchild":             print("object of type \(obj.title)")         default :             print("object of type \(obj.title)")     } } 

Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -