Skip to content

Instantly share code, notes, and snippets.

@rosstulloch
Created October 25, 2017 20:58
Show Gist options
  • Save rosstulloch/508ddd6606b0072b1e11a27e2705913f to your computer and use it in GitHub Desktop.
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.
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