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

来源:互联网 发布:excel图表数据区域 编辑:程序博客网 时间:2024/05/17 16:45

在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 gcdmainCompletion Shortcut: @gcdmain Completion Scopes: Function or Methoddispatch_async(dispatch_get_main_queue(), ^{    <#code#>});
Title: GCD: Dispach gcdglobalCompletion Shortcut: @gcdglobal Completion Scopes: Function or Methoddispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{    <#code#>});

dispatch_after自带

dispatch_once自带


Title: Static NSStringCompletion Shortcut: @staticstring Completion Scopes: Top Levelstatic NSString* const <#name#> = <#value#>;
Title: Static NSIntegerCompletion Shortcut: @staticint Completion Scopes: Top Levelstatic const NSInteger <#name#> = <#value#>;
Title: NSLogCompletion Shortcut: @log Completion Scopes: Function or MethodNSLog(@"<#Log#>");
Title: weakselfCompletion Shortcut: @weakself Completion Scopes: Function or Method__weak typeof(self) weakSelf = self;
Title: strongSelfCompletion Shortcut: @strongSelf Completion Scopes: Function or Method__strong typeof(<#weakSelf#>) strongSelf = <#weakSelf#>;
Title: sharedInstanceCompletion 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: tableinitCompletion 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: tableDelegateCompletion 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: buttoninitCompletion 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: borderButtonCompletion 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: labelinitCompletion 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: attributedLabelCompletion 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.gitcd 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

0 0
原创粉丝点击