1

I want to write a macro to generate 2 methods like below:

- (NSString*)propertyName { NSString *key = @"PropertyName"; //get value from NSUserDefaults //... } - (void)setPropertyName:(NSString*)value { NSString *key = @"PropertyName"; //set value to NSUserDefaults //... } 

The first letter of property name is lower case in the get method, upper case in the set method and key value.

The macro should receive 1 or 2 arguments which is the property name:

MY_PROPERTY(propertyName) 

or

MY_PROPERTY(PropertyName) 

or

MY_PROPERTY(propertyName, PropertyName) 

The argument is also the value for key (string value).

How to write a macro for this? I prefer the first or second one. Thanks.

11
  • 2
    Why have people marked this question down? (It's at -3 at the time of my writing this. I'm upvoting it now. Seems like a fair question to me.) Commented Jun 2, 2014 at 5:15
  • 1
    Why do you want such macro? Your setter doesn't set anything, your getter is short, it's easier to write it as usual. And it's way easier to support later. By the way, your getter also doesn't return anything :) Macro would just replace MY_PROPERTY with something, you wouldn't be able to edit ...s. May be you mean a snippet? (P.S. I didn't downvote you) Commented Jun 2, 2014 at 5:52
  • 1
    @BergQuester + FreeNick note that if there's something constant that you want in the ... this is incredibly useful. For instance if you have a PreferencesManager and you want to set and get properties from the NSUserDefaults then a macro would be very nice Commented Jun 2, 2014 at 6:18
  • 1
    My app has a lot of settings saved in NSUserDefaults. I want each setting has its own getter/setter. I don't want to duplicate that code everywhere. The problem is I don't know how to: - Pass an argument and change its first letter case. - Prepend the word set before the name in the setter. - Place the property name in a string like this: @"PropertyName". That's why you see ... in the code, it does not relate to the question. Commented Jun 2, 2014 at 6:59
  • 2
    @Arnol — You might want to edit your question to add some background explaining the purpose (e.g., NSUserDefaults), as it helps us understand your motivation much better. As I suspected, your motivation seems well-founded. :) Commented Jun 2, 2014 at 7:27

1 Answer 1

4

Let's get mad:

#define STRINGIFY(__TEXT__) [NSString stringWithFormat:@"%s", (#__TEXT__)] #define GENERATE_GETTER_AND_SETTER(__UPPER_CASE__, __LOWER_CASE__, __TYPE__) \ - (void)set##__UPPER_CASE__:(__TYPE__)__LOWER_CASE__ { \ NSString *propertyName = STRINGIFY(__UPPER_CASE__); \ \ ... } \ \ - (__TYPE__)__LOWER_CASE__ { \ NSString *propertyName = STRINGIFY(__UPPER_CASE__); \ \ ... return ... \ } \ 

Usage:

GENERATE_GETTER_AND_SETTER(MyProperty, myProperty, NSArray*) 

Note that you have to specify both lower case and upper case names and you have to know the type of the property.

It might be easier to declare the properties as @dynamic and then implement the methods dynamically, see Objective-C forwardInvocation: for more details.

Sign up to request clarification or add additional context in comments.

4 Comments

xcode generates getters and setters anyway so .. why?
@Daij-Djan Don't ask me. I expect he wants to do some other actions in them, e.g. redirect them into a dictionary instead of ivars. For me it's just an interesting exercise in macros :)
@Daij-Djan - from the code in the question the aim is to wrap NSUserDefaults as properties.
which is why I up voted this -- though I agree with the deleted 'answer' that macros should be avoided: qualitycoding.org/preprocessor

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.