用好代碼塊,提供開發效率

相信很多開發者在初級階段時都免不了記不住方法等各種各樣的窘境,于是,很多時候,在遇到使用相同控件屬性時,苦于記不住其種類繁多的代理方法,就只能照著之前寫過的代碼再照搬一遍.但是,好在蘋果公司早就已經為開發者考慮到了這一點,在Xcode中為開發者準備好了“快捷方式”——代碼塊

代碼塊,顧名思義,就是一“塊”嵌入的代碼框架,提前將所需的代碼框架寫入代碼塊,僅留出可能發生改動的地方用占位符代替,使用時,以自定義標記的按鍵呼出相應代碼塊,填寫所需占位符即可完成高效率的開發。

具體效果如下圖所示:

效果圖
怎么樣,是不是很炫酷,那么具體是怎么實現的呢?
1.首先,我們要現在類當中將我們所需的代碼寫好,以剛才我所使用的tableView的代理方法為例:
#pragma mark -
#pragma mark - tableView
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}

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

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

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

}

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

//注:占位符的書寫格式為<#name#>**
2.寫好代碼之后,我們找到Xcode的右下角,如圖的方式,找到代碼塊的存放處
看這個圖片
3.這些便是我們存放代碼塊的地方,Xcode中提前已經準備了一些系統自帶的方法,然后,我們需要做的就是將我們寫好的代碼全部選中然后丟進存放代碼塊的地方.
注意操作哦
Title就是你這段代碼在儲存點要給展示出來的名字,圖上標注的地方就是你呼出它所需鍵入的縮寫,隨便什么都可以,想些什么些什么,當然越短越好,這樣,就大功告成了下次需要使用的時候就只需打出你的縮寫,這段代碼就自己調出來了
4.嘗試呼出你新建的代碼塊,就如最開始我做的那樣,如果代碼塊數量不多,也可以直接從儲存點直接將其拖出來使用,像最開始存放時做的一樣,只不過我們是反過來拖出來
5.如果需要對已經存好的代碼塊進行修改,那么只需要找到你的代碼塊,然后單機它,點擊edit即可,如果想要刪除代碼塊,只需要選中代碼塊,然后輕敲Backspace鍵,彈出選項框時選擇delete即可

這里我總結了一些常用的代碼塊

1.copy:
@property (nonatomic,copy) NSString *<#string#>;
2.strong:
@property (nonatomic,strong) <#Class#> *<#object#>;
3.weak:
@property (nonatomic,weak) <#Class#> *<#object#>;
4.assign:
@property (nonatomic,assign) <#Class#> <#property#>;
5.delegate:
@property (nonatomic,weak) id<<#protocol#>> <#delegate#>;
6.block:
@property (nonatomic,copy) <#Block#> <#block#>;
7.mark:
#pragma mark <#mark#>
8.gmark:
#pragma mark - <#gmark#>
9.warning:
#warning <#message#>
10.ReUseCell:
 static NSString *rid=<#rid#>;

  <#Class#> *cell=[tableView dequeueReusableCellWithIdentifier:rid];

  if(cell==nil){

  cell=[[<#Class#> alloc] initWithStyle:UITableViewCellStyleDefault      reuseIdentifier:rid];

  }

  return cell;
11.initObj:
  if(self=[super init]){
<#init#>
  }

  return self;
12.dataFill:
  -(void)dataFill:(<#ModelClass#> *)<#model#>{

<#code#>

  }
13.MainGCD:
dispatch_async(dispatch_get_main_queue(), ^{
<#code#>
  });
14.GlobalGCD:
 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
<#code#>
});
15.AfterGCD:
 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(<#delayInSeconds#> * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
<#code to be executed after a specified delay#>
});
16.OnceGCD:
 static dispatch_once_t onceToken;
 dispatch_once(&onceToken, ^{
<#code to be executed once#>
 });

當你需要換電腦的時候,怎么把Xcode上之前自定義代碼塊遷移到其他電腦上呢?
首先 Command + Shift + G.前往如下路徑的文件夾
~/Library/Developer/Xcode/UserDa ta/CodeSnippets
然后把文件夾內部的文件復制,粘貼到另
-臺電腦的Xcode同樣的文件夾中即可
然后重啟xcode就可以了 .

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

推薦閱讀更多精彩內容