ios - 如何知道一个对象是子类还是父类(super class)的实例

标签 ios swift

我有 2 个类:

class Parent
{
  func a() {
     self.b()
  }
  func b() {
     // I want to check here
     if self is Parent  // warning "'is' test is always true"
     {
        log("instance of parent")
     }
  }
}
class Child:Parent
{
}

我想这样检查

//
var child = Child()
child.a()  // don't see log
var parent = Parent()
parent.a()  // see log

我知道我可以在父类(super class)中创建类似description 的方法,并在子类中覆盖它。 我想知道 Swift 是否可以在不执行 description

的情况下检查它

谢谢你的帮助

最佳答案

其实很简单,使用is关键字。

if child is Child

关于ios - 如何知道一个对象是子类还是父类(super class)的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34487276/

相关文章:

ios - 用Core Graphics绘制UIPageControl Indicator?

IOS,如何将语言环境强制为一种语言

swift - DepthData - 获取每像素深度数据(CVPixelBuffer数据分析)

ios - 将 UIScrollView 放在 UIPageView 中,滚动不起作用

快速完成和循环问题

ios - Firebase - 跨多个位置的原子写入不起作用

ios - 代码错误 : cannot overwrite dSYM file

ios - XCode 要我在创建 PFObject 子类的新实例时指定一个类名

ios - 核心数据,每次使用相同的登录电子邮件地址和密码登录时,userID 都会取另一个值?

javascript - iOS 设备的远程访问