13

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;

1 Answer 1

20

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.

Sign up to request clarification or add additional context in comments.

3 Comments

This creates a generic, id object.. How can you instantiate a "fully qualified", dot-addressable instance of the class. That is the question..
First, that was not the question, the question was can you create instances of the class which this answers. Once you start using reflection you can not do what you are asking unless you cast it to its fully qualified type.
and how to call methods of this instance?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.