iOS開發-代碼塊Code Snippets

問題描述:Xcode9之后如何拖拽選中文字之后拖拽代碼?
解決辦法:點擊選中的代碼(文字),不要移動和松開鼠標左鍵,三秒鐘之后,當豎線變成箭頭之后就可以拖動了。

一:初識代碼塊

比如最常見的定義屬性:

@property (nonatomic, strong) <#type#> *<#value#>
// 備注:<##> 作用是占位,## 之間可以輸入提示文字。

代碼塊在哪個地方:


代碼塊.png

雙擊編輯其中的某一個代碼塊(代碼塊就是{}中的):


代碼塊編輯.png

每個標題的含義為:
Title:標題。
Summary:描述文字。
Platform:可以使用的平臺(如iOS)。
Language:可以在哪些語言中使用(如 Objective-C)。
Completion Shortcut:快捷方式,以字母開頭(支持少數符號,如@)。
Completion Scopes:作用范圍,一般寫在正確的位置拖動即可,Xcode會自行選擇好。

備注:
1.1 點擊Edit對代碼片段進行編輯
1.2 點擊Done對代碼片段完成編輯
1.3 選中某一行代碼片段,點鍵盤擊Delete即可刪掉

二:常用代碼片段

1.注釋(屬性注釋)
Completion Shortcut: @explain property - 屬性注釋
Completion Scopes: Class Implementation 

/** <#注釋#> */ 
2.注釋(方法內部注釋)
Completion Shortcut: @explain single - 函數內部注釋
Completion Scopes: Class Implementation 

// --<#說明#>
3.注釋(函數mark注釋)
Completion Shortcut: @explain mark - 函數mark注釋
Completion Scopes: Class Implementation 

#pragma mark <#mark#>
4.GCD:主線程
Completion Shortcut: @gcdmain 
Completion Scopes: Function or Method

dispatch_async(dispatch_get_main_queue(), ^{
    <#code#>
});
5.待處理事項
Completion Shortcut: @warning 
Completion Scopes: Function or Method

 #warning todo <#message#>
5.GCD:異步
Completion Shortcut: @gcdglobal 
Completion Scopes: Function or Method

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    <#code#>
});
6.靜態字符串
Completion Shortcut: @staticstring 
Completion Scopes: Top Level

static NSString* const <#name#> = <#value#>;
5.靜態整形
Completion Shortcut: @staticint 
Completion Scopes: Top Level

static const NSInteger <#name#> = <#value#>;
6.輸入函數
Completion Shortcut: @log 
Completion Scopes: Function or Method

NSLog(@"<#Log#>");
7.若引用
Completion Shortcut: @weakself 
Completion Scopes: Function or Method

__weak typeof(self) weakSelf = self;
8.強引用
Completion Shortcut: @strongSelf 
Completion Scopes: Function or Method

__strong typeof(<#weakSelf#>) strongSelf = <#weakSelf#>;
9.單例類
Completion Shortcut: @instance 
Completion Scopes: Class Implementation

+ (instancetype)sharedInstance
{
    static id _sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedInstance = [[self alloc] init];
    });
    return _sharedInstance;
}
10.初始化tableView
Completion Shortcut: @tableinit 
Completion Scopes: Function or Method

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}
11.tableView代理和數據源方法
Completion Shortcut: @tableDelegate 
Completion Scopes: Class Implementation`

 #pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return <#count#>;
}

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

}

 #pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return <#rowHeight#>;
}
12.初始化button
Completion Shortcut: @buttoninit 
Completion Scopes: Function or Method

UIButton *button = [[UIButton alloc] init];
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];
13.初始化邊線button
Completion Shortcut: @borderbutton 
Completion Scopes: Function or Method

UIButton *borderButton = [[UIButton alloc] init];
borderButton.layer.borderColor = [UIColor <#color#>].CGColor;
borderButton.layer.borderWidth = <#borderWidth#>;
borderButton.titleLabel.font = [UIFont <#font#>];
borderButton.clipsToBounds = YES;
borderButton.layer.cornerRadius = <#cornerRadius#>;
borderButton.backgroundColor = [UIColor <#backgroundColor#>];
[borderButton setTitleColor:[UIColor <#titleColor#>] forState:UIControlStateNormal];
[borderButton setTitle:<#title#> forState:UIControlStateNormal];
[borderButton addTarget:self action:@selector(borderButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[<#code#> addSubview:borderButton];
14.初始化label
Completion Shortcut: @labelinit 
Completion Scopes: Function or Method

UILabel *label = [[UILabel alloc] init];
label.font = [UIFont <#font#>];
label.text = <#text#>
label.textColor = [UIColor <#textColor#>];
label.textAlignment = NSTextAlignmentCenter;
label.backgroundColor = [UIColor clearColor];
[<#view#> addSubview:label];
15.初始化可變label
Completion Shortcut: @attributedLabel 
Completion Scopes: Function or Method

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];

三:Git管理

Xcode中的代碼片段默認放在下面的目錄中:

~/Library/Developer/Xcode/UserData/CodeSnippets 

四:同步代碼片段

上述目錄設置成一個 Git 的版本庫,將代碼片段放到 Github 上。(備注:還未嘗試)

git clone git@github.com:SilenceLee17/xcode_tool.git
cd xcode_tool
./setup_snippets.sh

五:擴展

系統自帶的有一些代碼片段,我們能不能修改?當然可以啦。
Xcode內置代碼片段目錄

/Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Versions/A/Resources/SystemCodeSnippets.codesnippets

注意:Xcode5.1之前是在這個目錄下

/Applications/Xcode.app/Contents/PlugIns/IDECodeSnippetLibrary.ideplugin/Contents/Resources/? SystemCodeSnippets.codesnippets

六:SystemCodeSnippets.codesnippets淺析

該文件為plist格式xml文件描述文件。
IDECodeSnippetContents為代碼片段的內容,修改即可達到目的。 IDECodeSnippetIdentifier唯一標示,重名即會覆蓋。 IDECodeSnippetCompletionPrefix類似Completion Shortcut,鍵值留空可屏蔽該片段。

七:Tips

自定義目錄不能有相同標識符的模板,否則Xcode啟動后會崩潰。
自定義母的模板標識符可以跟系統默認模板標識符相同,可以達到覆蓋效果。
若要使用自定義模板覆蓋系統模板,則必須有DECodeSnippetUserSnippet字段,定義為true,否則Xcode啟動后會崩潰。

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

推薦閱讀更多精彩內容