-(UILabel *)toastLabel {
if(!_toastLabel){
_toastLabel = [[UILabel alloc] init];
_toastLabel.text = @"验证码已经发送到+86";
_toastLabel.font = [UIFont systemFontOfSize:12.0];
}
return _toastLabel;
}
这种代码基本上每次定义一个变量,都是需要的,那能不能根据定义的变量自动生成呢,至少要是这样
-(UILabel *)toastLabel {
if(!_toastLabel){
_toastLabel = [[UILabel alloc] init];
}
return _toastLabel;
}
在 appcode 中,定义变量后,利用 alt+回车 快捷键 -> 选择弹窗中的 Implement accessor methods -> 选择 toastLabel-> 自动生成了定义方法
- (UILabel *)toastLabel {
return _toastLabel;
}
但是这不是我们想要的
1、点击 AppCode->选择 Preferences
2、找到 File and Code Templates -> 选择code -> 找到 OC Property Getter Body
里面的代码是
#if ($IVAR_IS_AVAILABLE == "true")
return $IVAR;#else
return $DEFAULT_RETURN_VALUE;#end
修改成自己想要的代码模式
#if ($IVAR_IS_AVAILABLE == "true")
if(!$IVAR){
$IVAR = [[$RETURN_TYPE.replace("*"," ") alloc]init];
}
return $IVAR;#else
return $DEFAULT_RETURN_VALUE;#end
最后再 alt+回车就生成自己想要的了
- (UILabel *)toastLabel {
if (!_toastLabel) {
_toastLabel = [[UILabel alloc] init];
}
return _toastLabel;
}