IOS development-Code block reuse (Xcode Code Snippets)

Source: Internet
Author: User

First, you must emphasize the importance of code reuse.

The importance of reuse: First, high production efficiency; second, high software quality; third, proper reuse can improve the maintainability of the system.

Code reuse is not only an elementary form of code reuse.

Traditional reuse: Code cutting and pasting reuse, algorithm reuse, and data structure reuse.

In an object-oriented language, features such as data abstraction, inheritance, encapsulation, and polymorphism allow a system to provide reusability at a higher level.

Abstract and inheritance relationships allow reuse of concepts and definitions. Polymorphism makes implementation and application reusable. Abstraction and encapsulation can maintain and promote the maintainability of the system. So that the focus of reuse is not focused on specific implementation details such as functions and algorithms, but on the most important abstract layers that contain macro business logic.

(The above content is taken from )


It can be seen that code reuse is very important.

Repeated code blocks often appear in the development of an application or in the coding process.

For example, a UIButton statement and a UITableView are designed with the same code.

At this time, code reuse is especially important.

Of course, we can copy from the code we have previously written. However, such a search process is unnecessary. Because xcode provides me with another simple method. ---- Code Snippets

In general, it is to put the code in the xcode code library and use it directly to improve development efficiency.


Before introducing how to use Code Snippets, we will introduce several common Code reuse methods.

1. encapsulate common code snippets into a class
When a piece of code appears frequently in one or more projects, it is encapsulated in a class. When used, you can directly PASS Parameters to implement functions, or directly put this type in another project for the same use;

2. Use macros

3. The following describes how to use Code Snippets. (See blog: http://iosdeveloper.diandian.com/post/2013-06-05/40051279020)

With code snippets, we can create reusable code blocks that can be easily used wherever needed. This saves the input operation and time. In addition, once you learn to use code snippets, you will find that you can create and expand your own code snippet library.

It is very easy to create a code snippet. First, open Xcode and select code snippet library in utilities panel.

In the code snippet library. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + CiA8L3A + signature/nKvqOs1Nq0 + signature + cjxpbwcgc3jjjpq = "http://www.2cto.com/uploadfile/Collfiles/20140329/2014032910534667.jpg" width = "800" height = "600" alt = "\">

Select a code block, as shown in:

Click and hold down the code block to know that the text cursor changes to the arrow cursor. Drag the code block to the code snippet library and release the mouse. As shown in

A popover is displayed, which allows you to edit the new code snippet, as shown in.

First, set the snippet name. Here I specify "Pragma Mark"

Then, specify the completion cut (optional) of the snippet ). Here I specify "pm ". After this setting, you can simply add this snippet to the code by entering the shortcut (pm) in the Xcode code editor. Very useful!

Next, we can see that in the above example, the title of pragma mark is "UIViewController overrides", but we want to modify it to easily enter any title.

We can simply change the text content contained in code snippet to "# pragma mark. However, there is a better way to encapsulate text blocks in the middle of "<#" and "#>", so code snippet will point out the full range of custom text that we can insert.

Next, replace "UIViewController overrides" with "<# Title #> ".

Note that the completion scopes field is not modified here. You can use this field to specify the effective range of completion shortcut.

Finally, click the edit button to complete snippet editing. Then you can see a preview of the final result in this popover screen.

Click done In popover. Below, you can delete the text lines written in the code editor to create snippet.

Now, try the snippet you just created! There are two methods. First, find the snippet in the code snippet library, and drag it to the code editor with the mouse...

... Then release the mouse.

Once you drag and drop snippet to the code editor, you can move the focus between different completion fields by clicking the tab key on the keyboard.

The second method is to simply enter the content set in completion cut in the code editor. Here we are "pm ".

Click the return key on the keyboard to insert snippet into the code editor.

Easy! Now that you know how to create your own snippet, you will find it very useful. You can add the same code block that you have repeatedly entered to your code snippets library at any time.



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.