Create instance of class known at runtime in Swift

后端 未结 3 1314
遇见更好的自我
遇见更好的自我 2021-02-14 14:00

A picture is worth a thousand words, how to rewrite this code from Objective-C to Swift?

- (id) instanceOfClass: (Class) class withInitializer: (SEL) initializer         


        
3条回答
  •  天命终不由人
    2021-02-14 14:41

    This cannot be done purely in Swift. You can only do this by creating the "class instance by name creator" in Objective C and calling this code from Swift.

    For more information you can read this article. http://ijoshsmith.com/2014/06/05/instantiating-classes-by-name-in-swift/

    And check out this github repo https://github.com/ijoshsmith/swift-factory

提交回复
热议问题