swiftswift-class

How to use multiple subclasses for function parameter


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())

Solution

  • 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
    }