Tojas
Tojas

Reputation: 21

Objective C class problem

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.

Upvotes: 0

Views: 201

Answers (2)

newacct
newacct

Reputation: 122439

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]]);

Upvotes: 0

Chuck
Chuck

Reputation: 237040

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.

Upvotes: 2

Related Questions