Convert a string (“MyExampleClass”) into a class name (MyExampleClass)

后端 未结 4 1574
面向向阳花
面向向阳花 2020-12-24 13:41

I want to convert a string to a class name. Imagine that I have a string, which changes, containing a class name, for example, the string \"MyExampleClass\". No

4条回答
  •  没有蜡笔的小新
    2020-12-24 14:07

    Here's what you'd want:

    Class theClass = NSClassFromString(classNameStr);
    id myObject = [[theClass alloc] init];
    

    Note that you can't use theClass as a type name (i.e. theClass *myObject). You'll have to use id for that.

提交回复
热议问题