Skip to content

Instantly share code, notes, and snippets.

@olegam

olegam/gist:8997427

Created Feb 14, 2014
Embed
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())
#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

@jonreid

This comment has been minimized.

Copy link

@jonreid jonreid commented Feb 18, 2014

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

@olegam

This comment has been minimized.

Copy link
Owner Author

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

This comment has been minimized.

Copy link

@AlexDenisov AlexDenisov commented Feb 27, 2014

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