Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8173039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:59:24+00:00 2026-06-06T21:59:24+00:00

In my project, I usually have a composite object (GameObject) that needs to expose

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T21:59:27+00:00Added an answer on June 6, 2026 at 9:59 pm

    Found a better solution. The trick was understanding how symbols are split up, to properly use ‘##’. Also, had to add a macro for the property declaration to customize the getter/setter names, which gets around the issue of capitalizing the name of the property. Not a big deal for me, helps me remember its a special property. The only downsize I can think of is they won’t be KVO compliant because of the custom name.

    I renamed the macros to make them easier to remember (they substitute for a @property and @synthesize respectively), and rearranged the argument order to mirror a property.

    #define PROPERTY_OF_IVAR(prop_type,ivar,prop_name) \
        @property (setter=set_##prop_name:, getter=get_##prop_name) prop_type prop_name \
    
    #define SYNTHESIZE_IVAR_PROPERTY(prop_type,ivar,prop_name) \
        - (prop_type)get_##prop_name { return ivar.prop_name;} \
        - (void)set_##prop_name:(prop_type)set_val { ivar.prop_name = set_val; } \
    

    Another version, makes @property declarations more natural, and has the added bonus of being able to a property at any depth (rather than just ivar properties, you can reach down to ivar.property.another_property.indefinitely). You can also set the name of the property to be different from its name in the composed object.

    // prop_type - type of property that will be exposed
    // prop_name - name of property in composing object
    #define PROPERTY_OF_IVAR(prop_type, prop_name) \
        (setter=set_##prop_name:, getter=get_##prop_name) prop_type prop_name \
    
    // prop_type - type of property that will be exposed
    // prop_path - path to the property you want
    // prop_name - must match PROPERTY_OF_IVAR (above)
    #define SYNTHESIZE_IVAR_PROPERTY(prop_type, prop_path,prop_name) \
        - (prop_type)get_##prop_name { return prop_path;} \
        - (void)set_##prop_name:(prop_type)set_val { prop_path = set_val; } \
    

    Example use:

    // foo.h
    @interface
    @property PROPERTY_OF_IVAR(CGPoint, position);
    ...
    
    // foo.m
    @implementation
    SYNTHESIZE_IVAR_PROPERTY(CGPoint, sprite.position, position);
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I usually have multiple copies of a project, for example: a copy of the
I have a project that usually works with Xcode 4, however since upgrading to
I have a script that copies some files into a project. Such a project
I have a unit test project based on UnitTest++. I usually put a breakpoint
I work on a project that takes 2-3 minuts to compile. Usually when I
I usually program in C++, but for school i have to do a project
I'm usually not working with Microsoft products. I have an MS Access 2007 project
I have a project that will basically be a large brochure html website. Although
So I have a few machines in production that are running a Sinatra app
I have a project that can be accessed at the URL: myapp.com/project/my-project Route To

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.