前言
我们在开发的时候会用到很多常用的代码,比如UITableView的代理,一般情况下我们要么自己敲要么复制粘贴,但是Xcode有一个功能,可以用一行代码敲出你预设的一段代码。这就是Xcode的代码块功能,这篇文章跟大家分享一些常用的和自定义的代码块,有需要的下面来一起看看吧。
一、常用的:
1.strong:
@property (nonatomic,strong) <#Class#> *<#object#>;
2.weak:
@property (nonatomic,weak) <#Class#> *<#object#>;
3.copy:
@property (nonatomic,copy) NSString *<#string#>;
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.ReUseCell:
static NSString *rid=<#rid#>;
<#Class#> *cell=[tableView dequeueReusableCellWithIdentifier:rid];
if(cell==nil){
cell=[[<#Class#> alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:rid];
}
return cell;
9.MainGCD:
dispatch_async(dispatch_get_main_queue(), ^{
<#code#>
});
10.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#>
});
11.OnceGCD:
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
<#code to be executed once#>
});
二、自定义代码片段:
以Strong为例:
1.在书写@property属性的地方写下如下语句:
@property (nonatomic,strong) <#Class#> *<#object#>;
2.选中上述语句,用鼠标左键拖到 下图中指示的代码片段在Xcode中的区域里,就新建了一个代码片段

3.松开鼠标左键的同时,会弹出代码片段编辑窗口,如下图所示:










