Small knowledge, big challenge! This paper is participating in theEssentials for programmers”Creative activities.

This paper also participates inProject DigginTo win the creative gift package and challenge the creative incentive money.

preface

A few days ago, I saw an article about digging friends that was so popular that it topped the home page for a long time. It was this article:

I went over and studied, and I admit, I was pretty sour when I saw the number of views, likes and replies, wondering why people get so much attention.

As soon as I saw that it was a reply, I could get the code block, and then I realized that the author was also an iOS educator.

However, it is better to teach a man to fish than to give him fish.

Although I am sour, but still participate in the activity, the article is still to write well.

Brace yourself and watch me teach you how to write code blocks.

A block of system code

First of all, we should be clear, in fact, the system has built a lot of code blocks for us, familiar with them, we can save a lot of time in the usual code writing:

So familiar with the system built-in code block, is very important, but also for us to write custom code block as a reference.

Let’s take a closer look at this Do catch block:

I have described the code block configuration in detail in the diagram, and I think the most critical thing is the placeholder in the body of the code block.

Let me illustrate this by writing a custom block of code.

Custom code blocks

This is the code block we want to customize. Let’s look at the function of this:

It can be used as a comment, but it can also be used to cut up the organization of the code, so it is very simple and very practical, can be said to be common code.

Ok, let’s start by generalizing it into custom code blocks:

  1. So let’s go ahead and select this code, right click, and clickCreate Code Snippet...

  1. Code block configuration:

  1. Placeholder writing:

//MARK: This is a delimiter, which does not meet the needs of custom blocks. It is not possible to delete this delimiter after each ma shortcut and then write the actual content.

This is done by using the <# placeholder description #>. To make it clear, I recorded a small GIF:

With <# and #>, write directly to placehold in these two places and you are done!

  1. call

I will be prompted when I type ma in the editor:

Backhand a return and the code block appears:

Fill in the placeholders:

Here’s a GIF to demonstrate:

advice

Here are some tips for writing and using custom blocks of code:

  • Code block or write their own better, is the so-called father and son soldier, tiger brothers, their own editing code block whether from the familiarity or shortcut keys or even the use of the scene, they are familiar, with it can be described as a thousand miles a day.

  • If you want to use other people’s code blocks, it is best to be familiar with and read after the import, otherwise do not know the purpose of the code and shortcut keys, import is useless.

  • Keep in mind that frequently used code is edited into blocks, and if you write blocks that are long and infrequent, you lose the value of blocks.

  • The code block name, such as OC code block with O, Swift code block with S development, you decide a suitable for their own rules.

  • Consider hosting blocks of code on GitHub and uploading them for backup periodically, so you can use them even when you change computers.

conclusion

Code block is the sharp edge of the code, this article starts from the system code block, through the analysis of configuration items, and then the actual combat custom code block, show the preparation of key placeholders, so that we understand how to write code block.

As said above, it is better to teach people how to fish than to teach them how to fish.

Finally, I gave you some advice on how to write and use blocks of code.

I hope you enjoy it and are welcome to discuss and share your own code blocks.

We’ll see you next time.