How to use multiple subclasses for function parameter

99 Views Asked by At

Say I have 2 subclasses of a class but would like to pass either of those subclasses to a function and determine its type within the function how would that be done?

class ParentClass {
  ...
}

class subClass1: ParentClass {
 ...
}

class subClass2: ParentClass {
 ...
}

function getClassType(type: ParentClass) {
   return(type)
}

viewDidLoad...
getClassType(type: subClass2())
1

There are 1 best solutions below

0
On BEST ANSWER

Ariel beat me to it but since i can't mark posts as duplicate yet ill post the answer.

The way to check if an object is op type is with the is keywoard:

if type is subClass1 {
    // Do stuff
}