Skip to content

Instantly share code, notes, and snippets.

Created February 14, 2014 08:08
What would you like to do?
AppCode code template to implement lazy-loaded property getters

This template let's you easily implement a property getter like this:

- (UIView *)myView {
	if (!_myView) {
		_myView = [UIView new];
	return _myView;

When you have declared a property like this:

@property(nonatomic, strong) UIView *myView;

You just change the OC Property Getter Body code template of AppCode to:

if (!$IVAR) {
#if ($RETURN_TYPE.endsWith("*"))
  #set($VAR_CLASS = $RETURN_TYPE.substring(0, $RETURN_TYPE.lastIndexOf("*")).trim())
$IVAR = [$VAR_CLASS new];
return $IVAR;

This solution was given to my by Dmitry Semeniouta from Jetbrains after I asked the question on their issue tracker:

Copy link

jonreid commented Feb 18, 2014

I had to add $ at the ends of variable names. Could you also show your settings for "Edit variables"?

Copy link

olegam commented Feb 18, 2014

It's not meant as a 'Live Template' but a 'Code Template' so you should not need to add $ and edit variables. Open Preferences->File and Code Templates->Code->OC Property Getter Body.

Copy link

I prefer to use ObjC runtime for this purposes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment