Programming in objective-c: C macro to expose property of ivar in objective-c on newest questions tagged objective-c – Stack Overflow

In my project, I usually have a composite object (GameObject) that needs to expose a few properties of an ivar into the GameObject’s interface. For example, a GameObject has a Sprite with a ‘position’ property, and I want to use the Sprite’s position as a property of the GameObject. This is easy enough with:

// GameObject.h
@interface GameObject : NSObject
@property CGPoint position;
...
@end

// GameObject.m
@interface GameObject ()
@property Sprite* sprite;   // private property
@end 

@implementation
- (CGPoint)position { return sprite.position; };
- (void)setPosition:(CGPoint)p { sprite.position = p; };
...

As a side project, I have been looking at generating the getter/setter with a C macro. Ideally I would be able to do:

@implementation
EXPOSE_SUBCOMPONENT_PROPERTY(subcomponent,propertyName,propertyType);
...

My latest failed attempt is:

#define EXPOSE_SUBCOMPONENT_PROPERTY(sub,property,type) \
- (type)property { id x = sub; return x.##property;} \
- (void)setProperty:(type)set_val { id x = sub; x.##property = set_val; } \

Any macro wizards out there able to help out?
Secondly, is there a way to not need to supply the type of the property to the macro?

See Answers


source: http://stackoverflow.com/questions/11148032/c-macro-to-expose-property-of-ivar-in-objective-c
Programming in objective-c: programming-in-objective-c



online applications demo