美文网首页
Xcode奇淫巧技(四)——代码块Code Snippets(转

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

作者: hypercode | 来源:发表于2018-10-26 00:20 被阅读0次

https://blog.csdn.net/a184251289/article/details/58032989
https://www.cnblogs.com/chebaodaren/p/4565772.html

Xcode的代码片段(Code Snippets)创建自定义的代码片段,当你重用这些代码片段时,会给你带来很大的方便。

### 常用的:

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:

<pre class="hljs objectivec" style="margin: 1.5em 0px; padding: 0px; font: 400 16px/1.5em &quot;Andale Mono&quot;, &quot;Lucida Console&quot;, monospace; width: 712.797px; overflow: auto; background: rgb(247, 247, 247); white-space: pre; color: rgb(37, 37, 37); letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">static NSString *rid=<#rid#>;  

 <#Class#> *cell=[tableView dequeueReusableCellWithIdentifier:rid];  

 if(cell==nil){  

 cell=[[<#Class#> alloc] initWithStyle:UITableViewCellStyleDefault      reuseIdentifier:rid];  

 }  

 return cell;</pre>

9.MainGCD:

<pre class="hljs lisp" style="margin: 1.5em 0px; padding: 0px; font: 400 16px/1.5em &quot;Andale Mono&quot;, &quot;Lucida Console&quot;, monospace; width: 712.797px; overflow: auto; background: rgb(247, 247, 247); white-space: pre; color: rgb(37, 37, 37); letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">dispatch_async(dispatch_get_main_queue(), ^{  
<#code#>  
  });</pre>

10.AfterGCD:

<pre class="hljs lisp" style="margin: 1.5em 0px; padding: 0px; font: 400 16px/1.5em &quot;Andale Mono&quot;, &quot;Lucida Console&quot;, monospace; width: 712.797px; overflow: auto; background: rgb(247, 247, 247); white-space: pre; color: rgb(37, 37, 37); letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> 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#>  
});</pre>

11.OnceGCD:

<pre class="hljs objectivec" style="margin: 1.5em 0px; padding: 0px; font: 400 16px/1.5em &quot;Andale Mono&quot;, &quot;Lucida Console&quot;, monospace; width: 712.797px; overflow: auto; background: rgb(247, 247, 247); white-space: pre; color: rgb(37, 37, 37); letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> static dispatch_once_t onceToken;  
 dispatch_once(&onceToken, ^{  
<#code to be executed once#>  
 });</pre>

## 自定义代码片段:

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

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

![1518951-56d8edf5f660204b.png](https://img.haomeiwen.com/i5362082/b151dec491883ad9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240 "1475142926382939.png")

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

![1518951-394fdb2756a05d90.png](https://img.haomeiwen.com/i5362082/125980bd5b24b9b2.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240 "1475142932182451.png")

图中从上到下的含义依次是:
①Title
代码片段的标题
②Summary
代码片段的描述文字
③Platform
可以使用代码片段的平台,有IOS/OS X/All三个选项
④Language
可以在哪些语言中使用该代码片段
⑤Completion Shortcut
代码片段的快捷方式,例:copy
⑥Completion Scopes
可以在哪些文件中使用当前代码片段,比如全部位置,头文件中等,当然可以添加多个支持的位置。
最后的一个大得空白区域是对代码片段的效果预览。
一切设置完成以后,点击该菜单右下角的Done按钮,新建工作就结束了。

## 代码片段备份:

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

`~/Library/Developer/Xcode/UserData/CodeSnippets`

我们可以将目录中的代码片段备份,也可以将其直接拷出来放在不同的电脑上使用。

相关文章

网友评论

      本文标题:Xcode奇淫巧技(四)——代码块Code Snippets(转

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