iOS development using code snippet to create snippets

Source: Internet
Author: User

Turn from: http://blog.csdn.net/hnsdwhl/article/details/46661541

This is for the collection of spare.

Add: Xcode7, select the code after the long press, the mouse changes the arrow, drag to the code snippet, and then edit save.


In the process of doing iOS development, you will find that some code in the project needs to be reused, in the Xcode inside we can use code snippet to create code snippets, and define a good shortcut, we need to enter the code, only need to enter a few letters, Can quickly generate this piece of commonly used code, this will greatly improve our programming efficiency.

Xcode inside the default definition of some of the more commonly used code snippets, such as we want to write a for in loop, we only need to enter the letters Forin, and then press the ENTER key, Xcode inside will automatically generate a for-in code block.



So Xcode inside the code snippet is defined, in the Xcode to the right of the navigation bar below, switch to the Second tab, you can see the Xcode with the default code snippet.


So how do we define our own code snippets, and the operation is simple. First, select the code fragment that you want to define, and then drag and drop it to the right snippet.

You can see that the code snippet automatically generates a snippet for us for the snippet, click the Edit button and we can set some of the properties of the code fragment. There are two main attributes need us to set up, modify the title of the default description, define completion shortcut shortcut Input method, which I created is a lable, I simply set to CL, click Done button, the end of this code fragment definition.

Now you just need to enter a shortcut (CL) in the Xcode code Editor to quickly add the code to the code.

The code block defined above, the font size and color are set to fixed, but in the actual use of the process, we generally have to modify the label font size and color, we can set these two values as placeholders, so that we can change the TAB key, quickly positioned to change the place. In code snippet you can place dynamically changing parameters between <# #>, such as the code block we defined above can be modified as follows:

[OBJC] view plain copy Uilabel *label = [Uilabel new];   Label.font = [Uifont systemfontofsize:< #fontSize #>];   Label.textcolor = [Uicolor < #textColor #>];   Label.backgroundcolor = [Uicolor Clearcolor];     [Self Addsubview:label]; Now we enter CL in the Code Editor to try again, found that set the font size and color of the place, has become a placeholder, we can switch through the tab to modify the corresponding value, this feeling than in the xib inside to set the properties of the control more convenient, there are wood ~


The name of the control created above is called lable, in the actual development process, we certainly need to modify the name of the control, but in the code block defined above, I do not have the name of the control through the <# #> defined as a placeholder, Because Xcode provides a shortcut to modify the same variable name, select the variable name, and then press control+command+e at the same time, you can change the name of the variable at once, if you use the placeholder, you have to fill in the name of the variable.


Code snippet is especially useful for laying out the UI with code, and because the layout of the codes requires you to create controls manually, if one goes to write very time-consuming, code snippet to create the control in the code block, where you need to use, You can generate the code that creates the control by using the shortcut key directly.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.