26

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

 -(void)FunctionInClass;

我正在使用名为 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.

我应该如何继续,我在 Objective-C for iPhone 应用程序中做这件事?

4

2 回答 2

56

1) what does NSClassFromString return??

它返回一个类,这意味着您可以[[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];
于 2010-02-09T03:55:10.473 回答
0

NSClassFromString() 是一个返回 Objective-C 类的函数。也就是说,在 Objective-C 中,一个类是一个可敬的对象。“类”类的一个实例......

Objective-C 类有什么用?

用于实例化对象(通过 alloc init )以进行自省和反射 - 查询该类的方法和属性实例向用户公开以用于归档/取消归档该类的实例,并且...能够运行类方法(不需要该类的任何特定实例的方法。

您的最后几行实际上创建了一个类的实例,其名称是传递给 NSClassFromString 的字符串。

于 2019-03-12T10:09:55.410 回答