SwiftLint style guide

A lint allows static analysis of the code, based on the rules defined in a syntactic guide.

In Swift, there are two particularly popular:

Ray Wenderlich Swift StyleGuide: the most complete and the most popular.

Github Swift StyleGuide: very popular and used in SwiftLint which we will talk about in a minute.

These guides detail a syntax to follow, which favors the readability of the code. This ranges from the number of lines to jump between each method, to the way to name the variables through the right way to unpack an optional one.

To discover SwiftLint go there

To install SwiftLint

With the package manager Homebrew

Once Homebrew is installed on your machine, in the terminal run the next remote control :

brew install swiftlint

That's all....

Add SwiftLint to your project

Our goal here will be to add a sequence to the compilation: the static analysis of SwiftLint. To add this sequence to the compilation, we will follow 5 steps:

1- Select the project in the browser

2- Select the target

3- Select the "build phases" tab

4- Press plus +

5- Choose "New Run Script Phase"

and change the compilation sequence by :

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

Capture d’écran 2021-05-18 à 19.52.26.png

Now use SwiftLint

build your project, and see the nice errors

Look at rule directory here

SwiftLint uses the two Xcode error levels:

the warnings that do not prevent the launch of the app

red errors that prevent the launch of the app

For example, if a line is too long and it exceeds 120 characters, we will have a warning. But if it exceeds 200, there it is really exaggerated and we get a red error.

If you are using a framework, you can exclude part of the code, or an entire file

import Foundation

// swiftlint:disable all

public protocol EventMonitor {
    /// The `DispatchQueue` onto which Alamofire's root `CompositeEventMonitor` will dispatch events. `.main` by default.
    var queue: DispatchQueue { get }

     ...
     ...

    open func request(_ request: DownloadRequest, didParseResponse response: DownloadResponse<URL?, AFError>) {
        requestDidParseDownloadResponse?(request, response)
    }
}
// swiftlint:enable all

More information here

have fuunnn