how to instantiate an object of class from string in Objective-C?

后端 未结 2 1503
傲寒
傲寒 2020-12-09 15:45

I\'ve a String who\'s value is the name of the Class[MyClass] which has to be instantiated, and MyClass has a method called

 -(void)FunctionInClass;
<         


        
2条回答
  •  醉梦人生
    2020-12-09 16:38

    NSClassFromString() is a function that returns an Objective-C class. That is, in Objective-C a class is an honourable object. An instance of the "Class" class...

    And what is an Objective-C class good for?

    for instantiating objects (via alloc init ) for introspection and reflection - to query it for the methods and properties instances of that class are exposed to the user for archiving/de-archiving instances of that class, and... to be able to run class methods (methods that do not require any specific instance of that class.

    your last lines actually create an instance of the class whose name is the string passed to NSClassFromString.

提交回复
热议问题