iOS 添加自定義代碼塊Core Snippet

在iOS開發過程中,經常會用到一些相似的代碼。比如常用到的TableView的代理方法,每個頁面每個類都要重復寫一遍,會不會覺得很麻煩,如果能把這些代碼塊保留下來,就像系統方法一樣,一鍵聯想就好了。那目前的Xcode能不能這樣做呢,答案是可以的。

下面就分開Xcode9和Xcode10來分別說明怎么添加自定義代碼塊。

Xcode9

在Xcode9中,core snippet模塊是放在右側邊欄的最下面,如圖所示


WX20181022-124014@2x.png

1、如何添加?

選中你想添加的代碼,比如


WX20181022-124241@2x.png

然后拖動到右下角Core Snippet區域,就會出現


WX20181022-124405@2x.png

其中
Title:標題。

Summary:描述文字。
Platform:可以使用的平臺(如iOS)。
Language:可以在哪些語言中使用(如 Objective-C)。
Completion Shortcut:快捷方式,以字母開頭(支持少數符號,如@)。
Completion Scopes:作用范圍,一般寫在正確的位置拖動即可,Xcode會自行選擇好。

但是建議你添加的這個Completion Shortcut,最好是以一個你自己固定的前綴,加跟你添加的這個方法名稱一樣或類似的作為后綴,比如viewDidLoad,你可以寫LZ_viewDidLoad類似這樣的,這樣的好處是當你編寫代碼的時候,寫viewDidLoad的時候既會聯想出系統的方法,也會聯想出你自己自定義的方法,方便選擇。


WX20181022-120235.png

2、如何編輯?

選擇你要編輯的代碼塊,然后再單擊你的代碼塊即可


WX20181022-124813@2x.png

3、如何刪除?

選擇你的代碼塊,然后shift+delete快捷鍵即可


WX20181022-124835@2x.png

Xcode10

在Xcode10中,core snippet模塊不再放在右側邊欄里,蘋果為了方便我們開發者,特地把這個常用模塊放到了頂欄中

WX20181022-115723.png

1、如何添加?

跟Xcode9不同,因為點擊后是個浮層,不能再像Xcode9那樣選擇代碼后左鍵拖到那個區域就能生成代碼塊。那在10中該如何添加呢?
第一種方法,點擊Edit->Create Code Snippet


WX20181022-115831.png

然后會出現一個浮層,添加你想添加的代碼塊上去即可,如圖所示


WX20181022-120235.png

但是建議你添加的這個Completion Shortcut,最好是以一個你自己固定的前綴,加跟你添加的這個方法名稱一樣或類似的作為后綴,比如viewDidLoad,你可以寫LZ_viewDidLoad類似這樣的,這樣的好處是當你編寫代碼的時候,寫viewDidLoad的時候既會聯想出系統的方法,也會聯想出你自己自定義的方法,方便選擇。


WX20181022-121957.png

添加好了之后,就能在Core Snippet里看到你添加的代碼塊了。

第二種方法,復制你的代碼,然后右鍵,選擇Create Code Snippet,這樣代碼就會自動放到Code Snippet里


WX20181022-123051.png

2、如何編輯?

選擇你要編輯的代碼塊,然后再單擊你的代碼塊即可


WX20181022-123442.png

3、如何刪除?

選擇你的代碼塊,然后shift+delete快捷鍵即可


WX20181022-123535.png

設置自定義代碼塊的好處

有了這些自定義的代碼塊,可以極大的幫助到我們快速開發。預先設置一些常用但是代碼量又很多的代碼放到Core Snippet里,會讓開發速度大幅提升。

下面簡單舉些例子??:

1、單例的創建

+ (instancetype)sharedInstance {
    static <#Class#> *_sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedInstance = [[self alloc] init];
        
    });
    return _sharedInstance;
}

2、創建tableView

- (void)setupTableView
{
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    [tableView registerClass:[<#classCell#> class] forCellReuseIdentifier:<#kReuseIdentifier#>];
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.dataSource = self;
    tableView.delegate = self;
    [<#view#> addSubview:tableView];
}

3、創建tableView的Delegate和DataSource

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return <#count#>;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    <#classCell#> *cell = [tableView dequeueReusableCellWithIdentifier:<#kReuseIdentifier#> forIndexPath:indexPath];
    
    return cell;
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return <#rowHeight#>;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

4、屬性

@property (nonatomic, copy) <#Class#> *<#name#>;
@property (nonatomic, weak) <#Class#> *<#name#>;
、、、

5、weakSelf、strongSelf

__weak typeof(self) weakSelf = self;
__strong typeof(<#weakSelf#>) strongSelf = <#weakSelf#>;

6、其他,比如常用的UIButton、UILabel等

UIButton *button = [UIButton buttonWithType:<#(UIButtonType)#>];
button.backgroundColor = [UIColor <#backgroundColor#>];
button.titleLabel.font = [UIFont <#font#>];
[button setTitle:<#title#> forState:UIControlStateNormal];
[button setTitleColor:[UIColor <#titleColor#>] forState:UIControlStateNormal];
[button addTarget:self action:@selector(<#btnClick:#>) forControlEvents:UIControlEventTouchUpInside];
[<#view#> addSubview:button];

一些常用UI控件或者組件,你可以自行進行添加,當你維護出一套自己的Core Snippet之后,你會發現,你的編碼效率會大幅度提升。

最后,建議自己整理一套Core Snippet,然后提到自己的github上,這樣在多臺設備上都可以隨時使用。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。