日常工作当中适当使用一些小技巧可以让效率倍增,对于一些重复编写的内容,比如属性,单例,列表等等,如果能像写if一样自动出来代码块的效果,那么会给你带来很大的方便。
步骤
在Xcode里选中需要设置快捷代码块的代码然后右键(我这里以单例为例),在弹出的菜单中选中Create Code Snippet
image
设置名称和快捷键,我这里将快捷键设置成ssdl
image
输入快捷键,检测是否有用
image
大功告成!接下来你就可以创建你想用的代码块了。
下面分享下几个大圣常用的代码块:
常用代码块
属性:
@property (nonatomic, weak) Class *<#object#>;
@property (nonatomic, strong) <#Class#> *<#object#>;
@property (nonatomic, copy) NSString *<#string#>;
@property (nonatomic, assign) <#Class#> <#property#>;
@property (nonatomic, weak) id<<#protocol#>> <#delegate#>;
@property (nonatomic, copy) <#returnType#>(^<#blockName#>)(<#arguments#>);
单例:
static <#class#> *singleClass = nil;
+ (instancetype)shareInstance{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
<#code to be executed once#>
});
return <#expression#>
}
GCD:
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#>
}
);
dispatch_async(dispatch_get_main_queue(), ^{
<#code#>
});
tableView:
static NSString *rid=<#rid#>;
<#Class#> *cell=[tableView dequeueReusableCellWithIdentifier:rid];
if(cell==nil){
cell=[[<#Class#> alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:rid];
}
return cell;
- (UITableView *)tableView {
if(!<#tableview#>) {
<#tableview#> = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
<#tableview#>.delegate =self;
<#tableview#>.dataSource =self;
}
return<#tableview#>;
}
#pragma mark - tableView delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return <#expression#>
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return <#expression#>
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identify =@"cellIdentify";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
if(!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identify];
}
cell.textLabel.text =self.arrayTitle[indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
mark
#pragma mark - <#mark#>