AppCode 設置 getter 模板

-(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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章