Raphael Raphael - 1 year ago 99
Objective-C Question

Objective C Preprocessor: Getting current class

Is there a way to get the class of the current scope in the preprocessor?

Currently, what I am doing is the following:

I have a macro:

#define DATA_SOURCE_DEF_CONSTR(CLASS) + (CLASS *)dataSource { \
CLASS *source = [[[CLASS alloc] init] autorelease]; \
return source; \

and then I am using that macro in a lot of classes like:


I would like to something like:

#define DATA_SOURCE_DEF_CONSTR + (__CLASS__ *)dataSource { \
__CLASS__ *source = [[[__CLASS__ alloc] init] autorelease]; \
return source; \

And call it like:

@implementation ...




Is that possible in Objective-C with the preprocessor?

Answer Source

I don't get what you're trying to accomplish. Why not just add a category to NSObject, like:

@implementation NSObject (handyConstructor)

+ autoreleasedInstance { return [[[self class] alloc] init] autorelease]; }


Is there some reason why you want this to be done by the preprocessor in particular?

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download