Code Snippets Library

Xcode is a Library that stores Snippets of Code. Allows users to quickly call up code snippets in the library with specific identifiers, reducing the writing time of high-frequency code snippets. In the Code Snippets Library, there are Snippets that are provided by default. These are common Code Snippets, such as defining a new block. But its biggest convenience is that you can add code snippets, so that we can according to their own coding habits to add their own often used code snippets, in their own coding process greatly reduce the manual compilation of repeated code, improve coding efficiency.

The Code Snippet Library in Xcode

I remember in older versions of Xcode, the Library panel for Xcode was in the bottom right part of the main xcode page, containing the file template library, the code slice shorts, the object library, and the media library. This is no longer the case in the new version of Xcode. There is no longer a fixed list of library panels, but a single type of library that is displayed at the top right of the main Xcode panel based on the type of file the user is currently writing. So for example when we’re writing a.h file or a.m file this is the snippet library. As for the specific version after which this change started, I haven’t checked. Anyway, xcode10 will be new after xcode10. I wonder if CODE9 will be new

Double clicking on the snippet we want at this point will quickly speed up the snippet at the point where we just edited it

Code Snippets Library creation, modification, deletion, and use

Code Snippets really work when they fit the user’s habits, which of course are the modified Snippets that the user creates. So it’s important to create and modify Snippets. Over time, users can have a library of Snippets that are very consistent with their usage habits, which greatly improves coding efficiency. If you can do better, according to a certain specification to create modified code snippets, and then through git and other management, shared by many people, can make the code base richer, more rapid improvement, such as a company ios development using the same code snippet library.

Creating a snippet is as simple as writing a snippet in a.h or.m file that you want to use as a snippet, such as defining a variable

@property (nonatomic, weak) NSString *name;

Select the entire line of Code, then right-click to open the menu bar and select Create Code Snippets from the menu bar. The edit panel appears

One of the things you can do is make some of the types optional, so you can make the code snippet more generic, so for example in this example here I define a variable of type NSString, so what if I want to define an Nsnumber, define a code snippet of type Nsnumber, that’s not a good idea, One type defines one snippet, and that’s a lot of snippets to write, and a lot to remember when you use them. In addition, the name of the variable should also be optional; you can’t have a “name” variable every time you call the snippet. So when defining code snippets, make some code optional based on generality. @property (nonatomic, weak) <#type#> *<#name#>; Modifying the code recreated in the snippet edit panel will now look like the following, or you can modify it directly in the edit panel

The snippet’s edit panel displays the snippet’s information: Title: The snippet’s name. Summary: Use the Platform, ios or macOs. By default, use all of them. Language: Takes effect in the programming Language. Completion is Shortcut: In this example, @snprppertyWeak means Snippet Property Weak, which is a code block for Weak Property. When used, just @snprppertyWeak can be written out quickly through auto-completion. Is used here @ beginning in order to avoid in the process of using the abbreviation of code snippets begins with engineering to some variables with the same name or in the beginning, save selection steps, and sn also said this is a code snippet in the beginning, so just three characters can make the calling code fragment won’t appear other irrelevant with options, lower efficiency. This is a personal habit that everyone can write according to their own habit. Completion Scopes: Scope.

To modify a written snippet that needs to be modified, simply bring up the snippet library panel, click the snippet you want to modify and wait for the snippet’s edit panel to appear.

If you need to delete the code snippet, also select the code snippet in the code snippet library, and then press the delete key on the keyboard, the prompt box for confirming deletion will pop up. Select delete.

Code Snippet Library management

Code Snippet Library file directory is ~ / Library/Developer/Xcode/UserData/CodeSnippets to directory can view the file to our snippets, created a piece of Code to the corresponding to a file.

When you open the file, you will find that it is in XML format, holding all the information for the code snippet

The end of the

This article only introduces the general use of CodeSnippet, does not provide common code snippets, interested in the Internet to find existing edited code snippets, add to their own development environment, can also slowly accumulate their own.