Trikalabs
  • Home
  • Best online TDD videos
  • Book Suggestions
  • About Me
  • Contact
Trikalabs
No Result
View All Result

Add SwiftLint to your iOS app

by fragi
July 26, 2019
in Tools
Share on FacebookShare on Twitter

Why linting is important:

Linting tools as SwiftLint do static analysis in our code. They will produce warnings or errors if they find that the code does not follow a predefined set of rules.

Linting tools can:

 1. Prevent bad practices, such as forced unwrapping

2. Improve the codebase style. All the project will follow the same style rules and that not only enhance readability but also removes potential conflicts amove engineers.

3. Help engineers focus on reviewing the actual code inside a pull request.

4. Help new team member learn the project style rules faster.

How to install SwiftLint:

The two most common ways to use SwiftLint in an iOS app is either installing it through cocoapods or installing it through Homebrew.

Installing it through cocoapods is a simple as adding the pod : pod ‘SwiftLint‘

into your podfile.

 

 In this article, we will install SwiftLint through Homebrew:

  1. Open the terminal and navigate to the root folder of your project
  2. Execute: brew install swiftlint

Let’s build first our project to see how many errors and warnings we have:

Great, currently we have 0 errors and zero warnings.

Now let’s add a new script in our project to run the swiftLint when we are building the project.

  1. Navigate to the build phases of the main target: 
  2.  Tap on the plus button to create a new run script:
  3. Add the following lines inside the script:
    Swift
    1
    2
    3
    4
    5
    if which swiftlint >/dev/null; then
      swiftlint
    else
      echo "warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint"
    fi

  4. Now, let’s build the project. The project fails to compile. There are 37 warnings and 2 errors:
  5. An easy way to fix the majority of the warning is to execute on the command line: swiftlint autocorrect. Let’s build again the project. Now we are having 3 warnings and 2 error. 

 

Disable and add custom Rules

If we look at the warnings and the errors, we will see that they are violating some SwiftLint rules.  In order to configure the rules of SwiftLint we have to create a swiftlint.yml file. In the terminal execute: 

touch .swiftlint.yml

Disable Rules

For disabling a rule we have two options.

A. Disable a rule for a single case:

All we have to do is add: // swiftlint:disable ruleName

To find the name of the rules you can type in the terminal: swiftlint rules

B. Disable a rule for the whole project:

Open the swiftlint.yml and add the following:

Swift
1
2
disabled_rules:
  - line_length

Custom Rules

 We can create our own rules if we like. Let’s add a rule that will throw an error if we use fatalError. Open the swiftlint.yml and add the following:

Swift
1
2
3
4
5
custom_rules:
  no_fatal_errors:
    name: "Don't use fatalError()"
    regex: "fatalError"
    severity: error

Exclude files

Some times we need to use cocoapods or carthage to add new functionality in our project. In order to avoid SwiftLint check this files we can open the swiftlint.yml file and add:

Swift
1
2
3
excluded:
  - Carthage
  - Pods

How to introduce swiftLint in an existing project:

Trying to use SwiftLint in an existing project can cause a lot of warnings and errors. The strategy I normally follow is:

  1. Execute swiftlint autocorrect to fix as many warnings as possible
  2. Disable all rules initially, and try to introduce one rules at a time.

Source code: 

https://github.com/Fragki/SwiftLint-Example

Resources:

https://github.com/realm/SwiftLint#installation

 

Tags: swiftlint
fragi

fragi

Related Posts

Delete Derived Data – zsh command
Tools

Delete Derived Data – zsh command

April 22, 2022

There are some times that we have to delete the derived data that Xcode produces.There are many ways to achieve...

SwiftGen – Statically reference resources
Tools

SwiftGen – Statically reference resources

February 28, 2022

In this article we can see how we can statically reference strings. Let's look on the following example: As...

Postman – Monitors
Postman

Postman – Monitors

February 27, 2022

So we have created our collection with the API request, we an up and running mock server http://trikalabs.com/postman-mock-server/  and a...

Xcode custom template
Tools

Xcode custom template

February 25, 2022

In this article we will create a template that creates a view and a viewModel files.In the previous article http://trikalabs.com/xcode-templates/...

Xcode Templates
Tools

Xcode Templates

February 24, 2022

Xcode provides with a lot of custom templates. In this article we will explore how we can change them. In...

Newman – Run Postman collections tests
Postman

Newman – Run Postman collections tests

February 27, 2022

Newman is a command-line Collection Runner for Postman. We can use newman to run postman collection tests through cli, which...

Next Post
Continuous Integration with TravisCI

Continuous Integration with TravisCI

Use Hound for automated code reviews

Use Hound for automated code reviews

Project Mess #1: No Localised Strings.

Project Mess #1: No Localised Strings.

  • Advertise
  • Privacy & Policy
  • Contact

© 2019 Trikalabs Ltd. All rights reserved.

No Result
View All Result
  • Home
  • About Me
  • A curated list with the best free online TDD videos
  • Book Suggestions
  • Pinner Code Club

© 2019 Trikalabs Ltd. All rights reserved.