-(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;
}