Xcode development skills of the Code Snippets Library

introduction

  In project development, we can often see the code as shown below:

1 @property (nonatomic, copy) NSString *isbatchapprove;
2 @property (nonatomic, copy) NSString *currentResult;
3 @property (nonatomic, copy) NSString *strVersion;
4 @property (nonatomic, copy) NSString *operateType;

  At first glance look, no problem, but in doing project development, in fact, is more trouble, because the same content as shown below, we need to write many times, virtually to our development increased workload. So is there any way to avoid such repetition of work it? This is the point here Code Snippets Library.

@property (nonatomic, copy) NSString

What is the Code Snippets 

  In fact, in the course of project development, we have many times to enjoy the convenience brought us the Code Snippets, such as shown in the following figure:

 

  When we entered the sw, so much behind the content will automatically appear, press the Enter key, the content will automatically write better. This is the Code Snippets  , object is to enable programmers to enter the fastest commonly used code fragments, to improve programming efficiency. This feature from start to introduce Xcode4, we can set the display Code Snippets in the following places:

  After setting the display, where we can see in the following Code Snippet, there are many Xcode own code fragment, is the embodiment in which a switch. Double-click on a specific Code Snippet, you can see the details of the Code Snippet, click the Edit button, you can see more content (the new Code Snippet when re-introduced )

How to Create Code Snippets

  Through the above description, for the benefits of Code Snippets, it should be very clear. After clearly useful, we have a problem: XCode can create your own Code Snippets do? The answer is yes, let's create a property through the introduction of a step by step introduction to Code Snippets process created.

  The first step: Writing Code Snippets . Wrote the following statement in place declared @property properties:

 

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

 

  The above <# name #> role in the use of the latter effect may be known.

  第二步:新建Code Snippets选中第一步中编写的语句,用鼠标左键拖到上图中指示的Code Snippets在Xcode中的区域里,就新建了一个Code Snippets。新建之后的效果如下所示:
  【属性说明】:
  • 1.Title:Code Snippets的标题;
  • 2.Summary:Code Snippets的描述文字;
  • 3.Platform:可以使用Code Snippets的平台,有IOS/OS X/All三个选项
  • 4.Language:可以在哪些语言中使用该Code Snippets
  • 5.Completion Shortcut:Code Snippets的快捷方式,比如本文第二部分编写switch代码段用到的sw,在这里,把属性设置的快捷方式设为pc
  • 6.Completion Scopes:可以在哪些文件中使用当前Code Snippets,比如全部位置,头文件中等,当然可以添加多个支持的位置。
  • 7.最后的一个大得空白区域是对Code Snippets的效果预览。
  一切设置完成以后,点击该菜单右下角的Done按钮,新建工作就结束了。
Code Snippets的使用
  我们有两种方式使用Code Snippets,还是以刚定义的pc属性Code Snippets为例:
  方式一:在适当的位置(因为定义代码片段的时候可以设置使用平台,使用语言,使用文件等限制条件),输入pc,即可弹出如下所示提示框。
 
  单击回车,即可出现如下代码:
  方式二:直接在Code Snippets部分找到我们需要使用的Code Snippets,用鼠标左键拖动该Code Snippets到合适的位置。
 

Code Snippets的备份

  Xcode中的Code Snippets默认放在下面的目录中:
~/Library/Developer/Xcode/UserData/CodeSnippets   
  我们可以将目录中的Code Snippets备份,也可以将其直接拷出来放在不同的电脑上使用,因此多台电脑之间的协作也毫无压力。

 

项目开发中常用的Code Snippets

  下面是我在项目中经常使用到的一些snippet,仅供参考。

  1.Notification Add:添加通知。

 

  2.Notification Remove:移除通知。

  3.Notification Post:发布通知。

  4.Property Copy:NSString类型的属性。

  5.Property Retain:retain属性。

  6........

转载于:https://www.cnblogs.com/eagle927183/p/3594292.html

Guess you like

Origin blog.csdn.net/weixin_34062469/article/details/93725199