Xcode奇淫巧技(四)——代码块Code Snippets

在iOS开发过程中,经常会用到一些相似的代码。我们能不能将这些代码保存起来,重复使用呢?回答是可以的。我们来看一下Xcode代码片段——Code Snippets。

初识代码片段

1. 新增

  • 书写如下语句
@property (nonatomic, strong) <#type#> *<#value#>;

<##> 作用是占位,## 之间可以输入提示文字。

  • 快捷键:cmd+opt+0

  • 将上述代码,鼠标左键拖到下图
    这里写图片描述

  • 弹出下图所示
    这里写图片描述

Title:标题。
Summary:描述文字。
Platform:可以使用的平台(如iOS)。
Language:可以在哪些语言中使用(如 Objective-C)。
Completion Shortcut:快捷方式,以字母开头(支持少数符号,如@)。
Completion Scopes:作用范围,一般写在正确的位置拖动即可,Xcode会自行选择好。


3. 修改

对代码片段进行修改,选中代码片段,点击edit即可。


4. 删除

对代码片段进行删除,选中代码片段,按delete键即可。


常用片段

@property之类的使用了插件,也可以Code Snippets自己写,不赘述。

Title: #pragma mark 
Completion Shortcut: @mark 
Completion Scopes: Class Implementation 

#pragma mark <#mark#>
Title: GCD: Dispach gcdmain
Completion Shortcut: @gcdmain 
Completion Scopes: Function or Method

dispatch_async(dispatch_get_main_queue(), ^{
    <#code#>
});
Title: GCD: Dispach gcdglobal
Completion Shortcut: @gcdglobal 
Completion Scopes: Function or Method

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    <#code#>
});

dispatch_after自带

dispatch_once自带


Title: Static NSString
Completion Shortcut: @staticstring 
Completion Scopes: Top Level

static NSString* const <#name#> = <#value#>;
Title: Static NSInteger
Completion Shortcut: @staticint 
Completion Scopes: Top Level

static const NSInteger <#name#> = <#value#>;
Title: NSLog
Completion Shortcut: @log 
Completion Scopes: Function or Method

NSLog(@"<#Log#>");
Title: weakself
Completion Shortcut: @weakself 
Completion Scopes: Function or Method

__weak typeof(self) weakSelf = self;
Title: strongSelf
Completion Shortcut: @strongSelf 
Completion Scopes: Function or Method

__strong typeof(<#weakSelf#>) strongSelf = <#weakSelf#>;
Title: sharedInstance
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;
}
Title: tableinit
Completion Shortcut: @tableinit 
Completion Scopes: Function or Method


    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    [tableView registerClass:[<#classCell#> class] forCellReuseIdentifier:<#kReuseIdentifier#>];
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.dataSource = self;
    tableView.delegate = self;
    tableView.backgroundColor=[UIColor colorWithHex:<#0xFFFFFF#>];
    [<#view#> addSubview:tableView];
Title: tableDelegate
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#>;
}
Title: buttoninit
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];
Title: borderButton
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];

Title: labelinit
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];
Title: attributedLabel
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启动后会崩溃。

摘录:

http://blog.csdn.net/minjing_lin/article/details/52688736
https://segmentfault.com/a/1190000005073808
http://www.jianshu.com/p/91a58380fb42
http://www.cnblogs.com/zhoup/p/5016729.html
http://www.jianshu.com/p/499e315a7667
http://www.jianshu.com/p/de7806f6143b
http://www.cnblogs.com/LiLihongqiang/p/5934677.html
https://github.com/tangqiaoboy/xcode_tool
http://www.cocoachina.com/industry/20130604/6336.html
http://www.cocoachina.com/ios/20160127/15004.html

猜你喜欢

转载自blog.csdn.net/a184251289/article/details/58032989
今日推荐