Skip to content

Instantly share code, notes, and snippets.

@superarts
Last active May 30, 2019 15:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save superarts/79dbaaf7828b9a4e6c9e8572ac4e1a99 to your computer and use it in GitHub Desktop.
Save superarts/79dbaaf7828b9a4e6c9e8572ac4e1a99 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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