Is it possible to generate setters / getters?

Sorry, sounds trivial, but I haven't found it ... is it possible to generate setter / getter code for an existing property?

0
12 comments

I know if the property hasn't been synthesized one of the options in the error fix menu is to generate the getters/setters.

0
Avatar
Permanently deleted user

Thanks, but this creates just empty methods. Do I really have to define the templates myself?

0

I'd argue against a default template for manually overriding getters/setters. A lot of the time when you're manually defining the getters/setters for a property, you're creating kind of a pseudo ivar that doesn't actually exist but updates other ivars in your class (common example - there is no "frame" ivar on UIView, and calling view.frame constructs a rect out of the view's center and bounds ivars).

With ARC nowadays, writing your own setter method isn't even that hard. All you have to do is say myVar = argumentVar and the compiler takes care of the rest.

0
Avatar
Permanently deleted user

To have templates inserted, choose "implement methods" (ctrl-i). Make sure that "show synthesized accessors" is checked, then select those which you want to implement (select multiple with cmd pressed).

These and other templates can be changed under settings -> file templates.

0
Avatar
Permanently deleted user

I see. This is what I tried at the beginning, but it didn't work so I thought there must be some other way. It generates just empty methods.
If I have

NSString *rabbit;

...


@property  (nonatomic, retain) NSString *rabbit;


AppCode generates

- (NSString *)rabbit {
    return nil;
    //To change the template use AppCode | Preferences | File Templates.

}

- (void)setRabbit:(NSString *)aRabbit {
    //To change the template use AppCode | Preferences | File Templates.

}


Is it a bug? The templates "OC Property Getter Body", "OC Property Setter Body" are there, I didn't change them.

0

AppCode can't generate the getter/setter bodies according to templates unless you have a @synthesize statement, which binds properties to ivars. So the algorithm is the following: first use "synthesize property" action to generate ivars, then "implement methods" to create the custom getter.

Is it a very popular combination of actions? If it is, we could combine them and skip the generation of @synthesize statement.

0

Maybe you can use defaults? By default, as far as I remember a property is named the same way as a field. It's not required to have @synthesize lines and getter/setter at the same time (if you have both).

0
Avatar
Permanently deleted user

Thanks for the explanation, Alexander. I see, now it works. But there are too many steps and too many keys to be pressed. I always liked Idea, because it makes things efficient.

I'd personally prefer the following way:

1) I manually create ivar
2) AppCode generates property with synthesise for me OR property with setter and getter (based on what I want)

Would this be possible? This way, I'd save more time.

0

Actually there are two steps and less than 10 keys :)
Ctrl + N; P (for froperties); Enter; Ctrl + I (implement); Enter

Or if your cursor is on the property:
Alt+Enter; Select "Synthesize property"; Enter; Ctrl + I (implement); Enter

Anyway I've created an issue for a combinated action: http://youtrack.jetbrains.net/issue/OC-2341. Please vote.

0

Since clang 4, all properties are implicitly synthesized, so since you have a property, you only need one action to generate the accessors - "implement methods".

0
Avatar
Permanently deleted user

Where do I get info on Clang 4? I only see info on Clang 3.

TIA

0

Try the latest Xcode 4.4 developers preview. Information is under NDA.

0

Please sign in to leave a comment.