Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@olegam
Created February 14, 2014 08:08
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olegam/8997427 to your computer and use it in GitHub Desktop.
Save olegam/8997427 to your computer and use it in GitHub Desktop.
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())
#else
  #set($VAR_CLASS = $RETURN_TYPE)
#end
$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: http://youtrack.jetbrains.com/issue/OC-9126

@olegam
Copy link
Author

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.

@AlexDenisov
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