SwiftLint, code of code, becomes the perfect paranoid

(1) what is SwiftLint?

SwiftLint, code of code, becomes the perfect paranoid
SwiftLint.jpg

Familiar with Python the students are no strangers to Pylint, Pylint is a Python code analysis tool, it analyzes the errors in the Python code, search does not comply with the code style standard (Pylint default code style is PEP 8, please refer to the reference information, and the potential problems of the code). Python is a very stressed format language, after all, people do not even have braces, look at Swift, does not seem to be in accordance with strict coding norms, there is no big problem Wrong! Writing code that is good in code style may be more important than writing efficient code. So SwiftLint was born. SwiftLint is a mandatory user a tool according to the Swift encoding Specification Guide Github to develop, it will be all not conforming to the Swift codes are marked by warning, some serious violation of the rules of the code and even let it not compile (country a red), I think is not very exciting?

(two) why do you need Lint?

1., the lone ranger dilemma

If you’re an individual developer, or the team doesn’t have CodeReview, then your code is definitely not perfect. People always make mistakes, especially when high speed iterations and rapid changes in business requirements make mistakes more likely to increase. The most embarrassing is that Xcode is very tolerant, even if your code does not conform to the standard, but if the grammar is no problem, then it will be particularly willing to let you compile without any warning.

However, whenever you’re a developer who’s on the go, you want your own code to be at least impeccable in its code style. However, this is inconsistent with the real world development needs and human nature, so this is a dilemma.

2.CodeReview war

Fortunately, the company you are in is a technology oriented company. Well, you must have a great deal of mutual Review. In my opinion, CodeReview usually does such a few things:

  • Code style specification unified
  • Prevents inefficient code and redundant code
  • Prevent visible visible BUG

Second, third points, which are full of a large number of human brain thinking, are temporarily unable to replace the operation (AI era should be possible). But the first point, really belongs to the mechanical operation, as if the first industrial revolution of artificial weaving, is in the ranks of being eliminated.

Under the tide of this era, SwiftLint came into being.

(three) installation;

No way, in accordance with the technical article of the three plate ax: Installation – use – step on the pit, it is also how can not go around the past chapter. I believe that readers are extremely clever, but for the integrity of the article I will Suo several.

(1) global installation

The global installation will have to refer to our old friend, HomeBrew, if you don’t have it installed, then look here.

Global installation is very simple. First we need to install the SwiftLint: through the brew command

Brew install swiftlint

Then add the compile script:

SwiftLint, code of code, becomes the perfect paranoid
global installation step diagram.Png

Finally, add the following script in the black box:

If which, swiftlint, > /dev/null; then swiftlint else echo; "warning: SwiftLint not installed"; "download from https://github.com/realm/SwiftLint" fi

That is

SwiftLint, code of code, becomes the perfect paranoid
Script.png

OK, you can compile it. After that, you can add scripts to the project every time you install it.

(2) partial installation;

In addition to global installation, we can also install through CocoaPods. Adding related dependencies on Podfile:

Pod'SwiftLint'

Then, like the global approach, you add commands in Run Script, but the content is somewhat different:

"${PODS_ROOT}/SwiftLint/swiftlint.""
SwiftLint, code of code, becomes the perfect paranoid
partial install.Png

OK, let’s try SwiftLint next!

(three) use

You may be intimidated by the following scenario when you first plug in the SwiftLint for your project:

SwiftLint, code of code, becomes the perfect paranoid,
, crazy Warning and Error.png

But, don’t panic, we can see what this is about. After a closer look, we’ll see that the vast majority of Warning is for this reason:

SwiftLint, code of code, becomes the perfect paranoid
image.png

WTF? Where is my line with a space character? What a mistake! But this is true. Although there seems to be no space character, the tabs should not be mixed between the newline characters, that is to say, your actual code is like this:

/n /tab /n

So, other people’s Warning is not without reason. So how do we get rid of these Warning?

First of all, we need to make sure that our code doesn’t appear that way any more, so we need to tick the Including whitespace-only lines inside the Text Editing:

SwiftLint, code of code, becomes the perfect paranoid
image.png

This ensures that our next line wrap no longer has tabs warning, and then we need to process the existing code. Fewer than tens of thousands of existing code, there are tens of thousands of lines, manual change is not tired? It was SwiftLint’s turn to play at this time:

SwiftLint, code of code, becomes the perfect paranoid
image.png

We switch the directory to the root of the project, and then tap the following command:

Swiftlint AutoCorrect

Then we’ll see that all the space characters “Warning” are gone. Thanks to the command line operation we just performed, it automatically fixes the known automatically repaired Error and Warning, greatly reducing our workload.

But there is a problem, perform automatic correction under the root directory of the project, SwiftLint will Pods Swift file in the folder with the correct frame, principle of third party can not move do not move, then what should we do?

.swiftlint.yml files are needed at this time. What document is this, then? When using SwiftLint, many times we encounter some custom rule requirements, and this time we need.Swiftlint.yml to solve the problem.

.swiftlint.yml

The so-called.Swiftlint.yml is actually a configuration file for SwiftLint, and we can modify the rules of the constraint through this configuration file to achieve the effect of customization.

The general configuration file looks like this:

SwiftLint, code of code, becomes the perfect paranoid
image.png

Here we introduce several main configuration options before, we must first understand the concept of a rules SwiftLint, the so-called rules is a style rules, such as a rule, the space character rules, type name rules etc.. Up to now, SwiftLint supports 75 rules altogether. If you’re interested, you can see all the rules in Source/SwiftLintFramework/Rules. Or you can enter swiftlint rules at the terminal to see the current rule information.

Disabled_rules

Disabled_rules: # disable the specified rules file_length...

Opt_in_rules

Opt_in_rules: # enabled the specified rule - file_length -...

Whitelist_rules

Whitelist_rules: # rules of the white list, but not with the above two rules together with the use of file_length...

Included

Included: # you want Lint search path, SwiftLint will scan all the.Swift suffix path of the file - /

Excluded

You don't want excluded: # path search, SwiftLint will ignore the path of the file - Pods

Style rule

Because there are too many rules in style, they don’t list here, but the usage is about the same:

Force_cast: [warning error] | when compulsory type conversion is Error or Warning
Type_body_length: - 300 # when more than 300 lines of 400 # when Huang Biao Biao red more than 400 lines

There are many rules. The usage is about the same. The reader is so intelligent that the old man needn’t speak much.

Comment control

There is a scene, sometimes, we do not want to disable a wide range of rules, but in a file, we must ignore this rule, so how should we tell SwiftLint to ignore it?

Very simple, notes!

For example, the following situation:

SwiftLint, code of code, becomes the perfect paranoid
image.png

Wow, this is the system to the agent method, but also warned me that the name of the method is too long! Ignore him? No, obsessive compulsive disorder can’t stand it!

So we can do that:

SwiftLint, code of code, becomes the perfect paranoid
image.png

OK, this will be ignored in swiftlint:disable line_length notes / / all the president warned. If you want to ignore this enabled again after a row, so as to add a line of swiftlint:enable / line_length on it.

SwiftLint, code of code, becomes the perfect paranoid
image.png

If you think there are a lot of control notes that don’t look good, personal recommendations can be written in the comments at the beginning:

SwiftLint, code of code, becomes the perfect paranoid
image.png

Or even more extreme:

SwiftLint, code of code, becomes the perfect paranoid
image.png

Nesting of configuration files

It is worth mentioning that, when we use the configuration file, SwiftLint will default to the specified file directory recursively scanning down. If a new.Swiftlint.yml file is found in the subdirectory during the scan, the rule in the subdirectory changes to the new configuration rule. It sounds very practical, but I have not used.

(four) stepping on the pit;

During my time using the Lint, I found two troublesome places to share.

Proposition number one

Note control is very common in actual use, so it is strongly recommended to use code blocks to simplify operations:

SwiftLint, code of code, becomes the perfect paranoid
image.png

Recommendation number two

In actual use, the creation of.Swiftlint.yml is also very frequent, especially if you are developed by modular, so whenever a new module or a Lint module to add, you need to create at least one YML file, so personal suggestions can add a command line for auxiliary SwiftLint.

I used Swift to simply implement a Maker and copy the executable file after it was copied to /usr/local/bin and then you can use it.

SwiftLint, code of code, becomes the perfect paranoid
image.png

After that, we can create a YML file quickly through the following command. We just need to make changes on the template file:

Maker -y
SwiftLint, code of code, becomes the perfect paranoid
image.png
SwiftLint, code of code, becomes the perfect paranoid
image.png

Compiling method

Some students say, do not know how to use Maker, here is a brief introduction, in fact, released APP similar:

1.Xcode compilation scenario

First, let’s turn on the Maker project and click Archive:

SwiftLint, code of code, becomes the perfect paranoid
image.png

Just a moment, our program is compiled, and then the steps are as follows:

SwiftLint, code of code, becomes the perfect paranoid
image.png
SwiftLint, code of code, becomes the perfect paranoid
image.png
SwiftLint, code of code, becomes the perfect paranoid
image.png
SwiftLint, code of code, becomes the perfect paranoid
image.png

Only two commands are needed at last:

SwiftLint, code of code, becomes the perfect paranoid
image.png
2. command line compiling scheme

The second approach is simpler by pure command:

CD path # path here refers to the main.swift main.swift Maker.swift -o Maker swiftc file directory # compiled binary file CP Maker /usr/local/bin # is copied to the target directory.

OK! Be accomplished! Come on, try Maker!

This to me this kind of memory is not very good for many people friendly o (~ ~ at) d, in fact the truth should be released HomeBrew or something more convenient, but the function is too simple, feel shy, just got engaged, after adding other features may be considered.

By the way, a lot of good features are moving away from us after the Xcode plugin is disabled, but that doesn’t mean that productivity is down. For example, the above command line tools, more aimed at throwing a brick, the main aim is to speed up our development efficiency, so do not just Swift as iOS development language Oh!

Ending

Thus, the use of SwiftLint for developers is also relatively good. Whenever there is a code of cleanliness iOS programmer, can try this SwiftLint, I believe you will not regret it. On the long job search, I hope you don’t get out of the interview because of the code style problem.

Finally, I wish all the world’s code to poetic elegant and tidy.