Created
October 25, 2017 20:58
-
-
Save rosstulloch/508ddd6606b0072b1e11a27e2705913f to your computer and use it in GitHub Desktop.
To instantiate a Swift class dynamically using NSClassFromSwift. This works for ObjC as well as pure Swift classes.
This file contains 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 YourClass { | |
required init() { | |
} | |
func saySomething() { | |
print("Hi!") | |
} | |
} | |
if let classType = NSClassFromString("APPNAMEORMODULE.YourClass") as? YourClass.Type { | |
let object = classType.init() | |
object.saySomething() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment