我有 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/