skspritenode - Get the type of AnyObject in Swift -


i have custom skspritenode subclass. name of class unit1.

in gamescene:

var allunit1:[unit1]? var enemy1 = unit1(imagenamed: "1") enemy1.position = cgpointmake(cgfloat(startpointx), cgfloat(self.frame.height))  // add custom node array. not relative  // question want describe of them.  if(allunit1 == nil) {     allunit1 = [enemy1] } else {     allunit1?.append(enemy1) }  self.addchild(enemy1) self.getdamage2unit(self.allunit1!.first!)   // function in gamescene. problem starts here. parameter anyobject  // see in below 

and getdamage2unit function (its in gamescene too);

func getdamage2unit(val:anyobject){      if(val.type == "unit1")         println("this anyobject unit1 objects")     } } 

this if condition doesn't work. looking that. need know anyobject's real type. how know that?

thank you

try this.

func getdamage2unit(val:anyobject){      if let mytype = val as? unit1 {         println("this anyobject unit1 objects")     } } 

now if know val of type unit1, change anyobject unit1

or

func getdamage2unit(val:anyobject) { let mytype = val unit1?          if mytype != nil {             println("this anyobject unit1 objects")         }     } 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -