美文网首页
常用的Xcode 代码块

常用的Xcode 代码块

作者: ZhangCc_ | 来源:发表于2017-04-13 16:26 被阅读290次

    不废话

    一、常用代码块

    1、strong:

    @property (nonatomic, strong) <#Class#> *<#object#>;

    2、weak:

    @property (nonatomic, weak) <#Class#> *<#object#>;

    3、copy:

    @property (nonatomic, copy) NSString *<#string#>;

    4、assign:

    @property (nonatomic, assign) <#Class#> <#property#>;

    5、delegate:

    @property (nonatomic, weak) id<<#protocol#>> <#delegate#>;

    6、block:

    @property (nonatomic, copy) <#Block#> <#block#>;

    7、mark:

    #pragma mark - <#mark#>

    8、ReUseCell:

    static NSString **cellID = <#cellID#>;
    <#Class#> *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

    if(cell == nil) {
    cell = [[<#Class#> alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    return cell;

    9、button:

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(<##>, <##>, <##>, <##>);
    btn.backgroundColor = [UIColor clearColor];
    [btn addTarget:self action:@selector(clicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];

    10、MainGCD:

    dispatch_async(dispatch_get_main_queue(), ^{
    <#code#>
    });

    11、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#>
    });

    12、OnceGCD:

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
    <#code to be executed once#>
    });

    13、Screen Size

    #define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
    #define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)

    二、自定义代码片段

    以Strong为例:
    1、在书写@property属性的地方写下如下语句:
    @property (nonatomic, strong) <#Class#> *<#object#>;

    2、选中上述语句,用鼠标左键拖到下图中指示的代码片段在Xcode中的区域里,就新建了一个代码片段

    1.png

    3、松开鼠标左键的同时,会弹出代码片段编辑窗口,如下图所示:

    2.png

    图中从上到下的含义依次是:

    1、Title

    标题

    2、Summary

    描述文字

    3、Platform

    可以使用代码片段的平台,有All/iOS/macOS/tvOS/watchOS五个选项

    4、Language

    可以在哪些语言中使用该代码片段

    5、Completion Shortcut

    敲出代码片段的快捷方式,例:copy

    6、Completion Scopes

    可以在哪些文件中使用当前代码片段,比如全部位置,头文件中,.m文件中,声明位置,实现位置等,当然可以添加多个支持的位置。

    最后的一个大得空白区域是对代码片段的效果预览。

    一切设置完成以后,点击该菜单右下角的Done按钮,新建工作结束。


    三、代码片段的备份

    Xcode中的代码片段默认放在下面的目录中:

    ~/Library/Developer/Xcode/UserData/CodeSnippets

    四、代码块备份

    下载代码块,直接拷贝文件夹到目录,无需任何改动!
    Github:https://github.com/CCBrother/CodeSnippets

    相关文章

      网友评论

          本文标题:常用的Xcode 代码块

          本文链接:https://www.haomeiwen.com/subject/vegbattx.html