tags:

views:

68

answers:

2

Hello!

I want to create a Class object in this way: Class c = [Class classNamed:[NSString stringWithFormat:@"%sview", [_shapeClass name]]];

My problem is this error message: "error: expected expression before 'Class'". What do you think? What's the problem? Thank you for replys.

Edit: I want to do this in a switch case.

+2  A: 

The problem is that there's no such class as Class (and as a side note, this nonexistent class also doesn't respond to classNamed:).

It's not quite clear whether you want to create a class or get a reference to a class. If the latter, you want the function NSClassFromString(). If you want to dynamically create a class at runtime, you'll need to use the Objective-C runtime functions to create a class, register it and add methods as appropriate.

Chuck
A: 

There is no class named "Class". "Class" is the type of a pointer to a class object. Anyway, there is a classNamed: method in NSBundle, which might be what you were looking for:

Class c = [[NSBundle mainBundle] classNamed:[NSString stringWithFormat:@"%sview", [_shapeClass name]]];

If you know the class is already loaded, you can also get it like this:

Class c = NSClassFromString([NSString stringWithFormat:@"%sview", [_shapeClass name]]);
newacct
When will a class in `[NSBundle mainBundle]` ever not be loaded?
Chuck