如何在Objective-C中从字符串中实例化类的对象?

sus*_*use 24 objective-c instantiation

我有一个字符串,它的值是必须实例化的类[MyClass]的名称,而MyClass有一个名为

 -(void)FunctionInClass;
Run Code Online (Sandbox Code Playgroud)

我正在使用名为NSClassFromString的方法来实例化MyClass.我想知道

1) what does NSClassFromString return?? 
2) what is id? 
3) How to call method -(void)FunctioninClass which is in MyClass using the instance.
Run Code Online (Sandbox Code Playgroud)

我该怎么办,我是在Objective-C for iPhone app中做的?

ech*_*cho 52

1) what does NSClassFromString return??

它返回一个Class,这意味着您可以[[NSClassFromString(@"MyClass") alloc] init]; 查看Mac Dev Center Docs以获取更多信息.

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];
Run Code Online (Sandbox Code Playgroud)