Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C: Creating Instance from Class Reference

You can create a class reference with the following code:

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

You can then 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;
like image 720
Alex Coplan Avatar asked Aug 16 '11 19:08

Alex Coplan


1 Answers

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.

like image 60
Joe Avatar answered Oct 02 '22 09:10

Joe