-1

I am new to swift. I was studying Inheritance. I have a method that takes in Character. Character is a super class here, and Player and Enemy are its subclasses. I would either pass Player or Enemy in the parameter of this method. How do I check which type of Character I am dealing with? Is it a Player or a Enemy?

func deadCharacter(character: Character) {}
Vandan Patel
  • 1,012
  • 1
  • 12
  • 23
  • 3
    `if character is Player { }` and `if character is Enemy { }`. You might want to pick a better name for your base class because `Character` is a basic Swift type. – vacawama Jul 20 '16 at 01:01

1 Answers1

1
if passedInCharacter.isKindOfClass(Player) {
    //do what you want to do with player
} else if passedInCharacter.isKindOfClass(Enemy) {
    //do what you want to do with enemy
}

I also agree with vacawama that you probably have to think of a better name for Character class.

Happiehappie
  • 1,084
  • 2
  • 13
  • 26