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

1 回答 1

0

阿里尔打败了我,但因为我不能将帖子标记为重复,但我会发布答案。

检查对象是否为 op 类型的方法是使用is关键字:

if type is subClass1 {
    // Do stuff
}
于 2017-01-08T22:46:00.517 回答