Get class by name
问题 Can I get a class by its name? For example: class Foo { } class Bar { } let x = "Foo" classByString(x) // need to return Foo I want to use metaprogramming to reduce code maintenance. 回答1: You can use a NSClassFromString : if let anyObj : AnyObject.Type = NSClassFromString("MyAppName.MySwiftClassFoo") { //call Foo } else { //call Bar } 来源: https://stackoverflow.com/questions/42081970/get-class-by-name