Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Getting and Comparing Type of Class
class BaseClass: NSObject {
func test() {
let aType = type(of: self)
if aType == dict["My Base"] {
print("this is base")
} else if aType == dict["My Inherited"] {
print("this is inherited")
}
print(aType)
}
}
class InheritedClass: BaseClass {
override func test() {
print("from inherited...")
super.test()
}
}
let dict = [
"My Base": BaseClass.self,
"My Inherited": InheritedClass.self,
]
BaseClass().test()
InheritedClass().test()
@superarts

This comment has been minimized.

Copy link
Owner Author

@superarts superarts commented May 30, 2019

Output:

this is base
BaseClass
from inherited...
this is inherited
InheritedClass

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment