How to create an instance of a class from a String in Swift

前端 未结 4 1358
终归单人心
终归单人心 2020-12-08 19:47

I have tried creating an instance of a class using a string in numerous ways with none of them working in Swift 3.

Below are pre-Swift 3 sol

4条回答
  •  情话喂你
    2020-12-08 20:08

    Apple provides a way to achieve this without having to use NSClassFromString.

    Bundle.main.classNamed("MyClassName")

    https://developer.apple.com/documentation/foundation/bundle/1407299-classnamed

提交回复
热议问题