2

If you have a known class type string, like for example @"NSString", you would create an instance of that class like so:

Class stringClass = NSClassFromString(@"NSString");
NSString* myString = [[stringClass alloc] init];

But what if you don't know the type of the class that you receive? I mean, you know it will be a valid one, you just don't know which one ...

Is it still possible to create an instance of that class, not id?

1 Answer 1

2

If you are calling [[unknownClass alloc] init] you will always create the actual class. You're only saving the class in an id typed variable. But the class itself remains in its type. You could also store a string in an id variable, but still use the string methods. E.g.:

id string = @"lalalala";
CGSize size = [string sizeWithFont: [UIFont systemFontOfSize: 12]];

Does this answer your question?

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

1 Comment

You don't need to do the performSelector dance, and in fact it distracts from your point that id types can be messages just as any other type.

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.