Last active
May 30, 2019 15:45
-
-
Save superarts/79dbaaf7828b9a4e6c9e8572ac4e1a99 to your computer and use it in GitHub Desktop.
Getting and Comparing Type of Class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output:
this is base
BaseClass
from inherited...
this is inherited
InheritedClass