0

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())
Sean Cook
  • 435
  • 7
  • 21
  • Is the parent class a subclass of NSObject, or is it a pure Swift class? – MacUserT Jan 08 '17 at 21:47
  • Take a look at [this answer](http://stackoverflow.com/questions/31231022/difference-between-swift-is-and-iskindofclass). Pretty much summarize the issue. – Ariel Jan 08 '17 at 21:50
  • Ariel thanks the "is" keyword was what I needed. I can mark your answer if you post. – Sean Cook Jan 08 '17 at 22:04

1 Answers1

0

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
}
Community
  • 1
  • 1
Emptyless
  • 2,964
  • 3
  • 20
  • 30