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.
...s. May be you mean a snippet? (P.S. I didn't downvote you)...this is incredibly useful. For instance if you have a PreferencesManager and you want to set and get properties from theNSUserDefaultsthen a macro would be very niceNSUserDefaults. 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 wordsetbefore 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.NSUserDefaults), as it helps us understand your motivation much better. As I suspected, your motivation seems well-founded. :)