开发者

Objective-C: Creating Instance from Class Reference

开发者 https://www.devze.com 2023-03-28 07:36 出处:网络
You can create a class reference with the following code: Class M = [NSMutableString class]; // NSMutableString (for example).

You can create a class reference with the following code:

Class M = [NSMutableString class]; // NSMutableString (for example).

You can the开发者_运维技巧n call methods on that saved class with code like this:

[M string];

But can you create instances, from that class name (I know the following doesn't work)?

M *newInstance;


You can allocate a new instance of the class like this

id instance = [[M alloc] init];

+alloc is a class method just like string is in your example so the rules are the same.

0

精彩评论

暂无评论...
验证码 换一张
取 消