xcode 常用代码块

在Xcode10正式发布之后,喜欢使用代码块的小伙伴会发现,原先位于编辑器右下角的代码块标识被放到上面了,点击 {}按钮之后会将所有的代码块弹出,如下图所示:


WeChatd27dcc79675a5d79ea1071dc1971d941.png

这时,相应的使用方法也随之发生了变化,例如,我们想创建一个生成
@property (nonatomic,strong) NSString *name; 的代码块

首先,我们需要选中这句代码,然后右键 --> Create Code Snippet 这时系统就会自动生成一个代码块,如下入所示
20181008180129642.png

然后,我们点击 {} 按钮 弹出代码块选择框,并将鼠标放置于刚才生成的代码块上,就会出现编辑框,这时我们就可以自定义代码块的 名称 和 快捷键,如下入所示:
20181008180150166.png

图中从上到下的含义依次是:
①Title
代码片段的标题
②Summary
代码片段的描述文字
③Platform
可以使用代码片段的平台,有IOS/OS X/All三个选项
④Language
可以在哪些语言中使用该代码片段
⑤Completion Shortcut
代码片段的快捷方式,例:copy
⑥Completion Scopes
可以在哪些文件中使用当前代码片段,比如全部位置,头文件中等,当然可以添加多个支持的位置。
最后的一个大得空白区域是对代码片段的效果预览。
一切设置完成以后,点击该菜单右下角的Done按钮,新建工作就结束了。

一、常用的属性

  1. assign
/** <#注释#> */
@property(nonatomic, assign) <#type#>  ;
  1. strong
/** <#注释#> */
@property (nonatomic,strong) <#Class#> *<#object#>;
  1. block
/** <#注释#> */
@property(nonatomic,copy) <#MyBlock#> <#blockName#>;
  1. weak
/** <#注释#> */
@property(nonatomic,weak) <#class#> *<#name#>;
  1. copy
/** <#注释#> */
@property(nonatomic,copy) NSString *<#name#>;
  1. BOOL
/** <#注释#> */
@property(nonatomic,assign) BOOL <#name#>;
  1. delegate
/** <#注释#> */
@property (nonatomic,weak) id<<#protocol#>> <#delegate#>;

二、 GCD相关

  1. OnceGCD
static <#class#> *singleClass = nil;

+ (instancetype)shareInstance{
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        <#code to be executed once#>
        
    });
    return <#expression#>
}
  1. 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#>  
});
  1. MainGCD
dispatch_async(dispatch_get_main_queue(), ^{  
    <#code#>  
    });

三、tableview相关

  1. table初始化
UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
tableView.frame = CGRectMake(<#x#>, <#y#>, <#w#>, <#h#>);
[tableView registerClass:[<#classCell#> class] forCellReuseIdentifier:<#kReuseIdentifier#>];
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
tableView.dataSource = self;
tableView.delegate = self;
tableView.backgroundColor=[UIColor colorWithHex:<#0xFFFFFF#>];
[<#view#> addSubview:tableView];
  1. tableViewDelegate
#pragma 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 {
    
}

四、通知

  1. 添加通知
[[NSNotificationCenter defaultCenter] addObserver:<#(nonnull id)#> selector:<#(nonnull SEL)#> name:<#(nullable NSNotificationName)#> object:<#(nullable id)#>];
  1. 移除通知
[[NSNotificationCenter defaultCenter] removeObserver:self];

五、注释

  1. 属性注释 - explain property
/** <#注释#> */
  1. 函数内部注释 - explain single
// --<#说明#>
  1. 函数注释 - explain mark
#pragma mark - **************** <#输入注释#> ****************

六、其他

1.警告 - warning

#warning TODO <#message#>
  1. 按钮 - btn
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(<#x#>, <#y#>, <#w#>, <#h#>);
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(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[<#view#> addSubview:button];
  1. 标签 - label
UILabel *label = [[UILabel alloc] init];
label.frame = CGRectMake(<#x#>, <#y#>, <#w#>, <#h#>);
label.font = [UIFont <#font#>];
label.text = <#text#>
label.textColor = [UIColor <#textColor#>];
label.textAlignment = NSTextAlignmentCenter;
label.backgroundColor = [UIColor clearColor];
[<#view#> addSubview:label];
  1. 自适应label - labelAttributed
UILabel *attributedLabel =[[UILabel alloc] init];
attributedLabel.numberOfLines = 0;
attributedLabel.preferredMaxLayoutWidth = <#preferredMaxLayoutWidth#>;
attributedLabel.backgroundColor = [UIColor clearColor];
NSString *text = <#text#>;
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
style.lineSpacing = <#lineSpacing#>;
NSDictionary *attr = @{
    NSFontAttributeName: [UIFont <#font#>],
    NSParagraphStyleAttributeName: style,
    NSForegroundColorAttributeName: [UIColor <#color#>]
};
attributedLabel.attributedText = [[NSAttributedString alloc] initWithString:text attributes:attr];
[<#view#> addSubview:attributedLabel];

你可能感兴趣的:(xcode 常用代码块)