如何在Objective-C中从字符串实例化类的对象? [英] how to instantiate an object of class from string in Objective-C?
问题描述
我有一个字符串,其值是必须实例化的Class [MyClass]的名称,而MyClass具有一个称为
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;
我正在使用称为NSClassFromString的方法来实例化MyClass.我想知道
i'm using the method called NSClassFromString to instantiate MyClass.I want to know
1) what does NSClassFromString return??
2) what is id?
3) How to call method -(void)FunctioninClass which is in MyClass using the instance.
我应该如何继续进行操作,我要在iPhone应用程序的Objective-C中执行该操作吗?
how should i proceed , i'm doing it in Objective-C for iPhone app?
推荐答案
1) what does NSClassFromString return??
它返回一个Class,这意味着您可以执行[[NSClassFromString(@"MyClass") alloc] init];
参见 Mac开发人员中心文档以获取更多信息.
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?
请参见 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];
这篇关于如何在Objective-C中从字符串实例化类的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!