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

后端 未结 2 1523
傲寒
傲寒 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:26

    1) what does NSClassFromString return??

    It returns a Class, which means you can do [[NSClassFromString(@"MyClass") alloc] init]; see Mac Dev Center Docs for more info.

    2) what is id?

    See http://www.otierney.net/objective-c.html#idtype

    3) How to call method -(void)FunctioninClass which is in MyClass using the instance.

    id myclass = [[NSClassFromString(@"MyClass") alloc] init];
    [myclass FunctioninClass];
    

提交回复
热议问题