代码块在iOS开发过程中,使用频率相当高,每个人都是用过,比如UIView的初始化方法之一:
- (instancetype)init
{
self = [super init];
if (self) {
<#statements#>
}
return self;
}
代码块是能够被重复使用代码,制作成模板,存放在Xcode的代码块库(Code Snippet library ())中,你可以使用Xcode提供的也可以自定义。
一、如何自定义代码块
1、选中你编写的代码片段,使用鼠标左键拖拽到Code Snippet library中
例如常见的UILabel使用
UILabel *label = [[UILabel alloc] init];
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor blackColor];
label.text = @"";
label.numberOfLines = 0;
label.font = [UIFont systemFontOfSize:15];
[self.view addSubview:label];
2、输入代码块名称
3、输入代码块简介
4、选择平台:All、iOS、macOS、tvOS、watchOS
5、选择语言:太多了不一一列举了
6、编辑简称:可以在编写代码时联想
7、选择使用范围(不翻译了,原文更容易理解)
All: Can be inserted anywhere.
Class Implementation: Can be inserted only within the scope of a class implementation.
Code Expression: Can be inserted only within the scope of an expression.
Function or Method: Can be inserted only within the scope of a function or method.
String or Comment: Can be inserted only within the scope of a string or comment.
Top Level: Can be inserted only at the top level of a source file.
8、编辑你选中的代码。可以通过<#输入你的名称#>格式添加在代码编辑时的高亮状态
二、编辑自定义代码块
1、在Code Snippet library中找到要编辑的代码块,双击--Edit
2、编辑相关内容
3、点击Done,完成编辑
二、使用自定义代码块
有两种使用方法
1、Code Snippet library中找到要使用的代码块直接拖拽
2、通过之前编辑的简称(completion shortcut),Xcode给出的及时联想,然后回车
3、有高亮效果的使用
四、结束
方法有了,肯定有很多玩法,欢迎交流有意思的玩法。