SwiftLint: Exclude file for specific rule

Solution 1:

Well, if you don't want some specific rules to be applied to a specific file you can use the technique mentioned by @Benno Kress. For that you need to add a comment in your swift file as given below.

The rules will be disabled until the end of the file or until the linter sees a matching enable comment:

// swiftlint:disable <rule1> 

   YOUR CODE WHERE NO rule1 is applied

// swiftlint:enable <rule1>

It is also possible to skip some files by configuring swiftlint. add a ".swiftlint.yml" file in the directory where you'll run SwiftLint.

Add the following content to exclude some files. Lets say file1, file2 ... etc

excluded: 
  - file1
  - file2
  - folder1
  - folder1/ExcludedFile.swift

To disable some rules completely add the following to the same ".swiftlint.yml" file.

disabled_rules: # rule identifiers to exclude from running
  - colon
  - comma
  - control_statement

for more information, refer the following links.

https://swifting.io/blog/2016/03/29/11-swiftlint/

https://github.com/realm/SwiftLint#disable-rules-in-code

Solution 2:

You can write // swiftlint:disable force_cast at the beginning of the file in which you want to supress the warning for this rule. It gets disabled until the end of the file or until you add the line // swiftlint:enable force_cast.

Source: https://github.com/realm/SwiftLint#disable-rules-in-code

Solution 3:

I just get rid with force_cast

Step 1:

cd path-to-your-project

Step 2:

touch .swiftlint.yml

Step 3: open .swiftlint.yml and add the rule

disabled_rules: # rule identifiers to exclude from running
 - force_cast

enter image description here

Reference - https://github.com/realm/SwiftLint#disable-rules-in-code

Solution 4:

Configure SwiftLint by adding a .swiftlint.yml file from the directory you'll run SwiftLint from. Here is the complete set of options you can use in your .swiftlint.yml file

disabled_rules: # rule identifiers to exclude from running
  - colon
  - comma
  - control_statement
opt_in_rules: # some rules are only opt-in
  - empty_count
  # Find all the available rules by running:
  # swiftlint rules
included: # paths to include during linting. `--path` is ignored if present.
  - Source
excluded: # paths to ignore during linting. Takes precedence over `included`.
  - Carthage
  - Pods
  - Source/ExcludedFolder
  - Source/ExcludedFile.swift
  - Source/*/ExcludedFile.swift # Exclude files with a wildcard
analyzer_rules: # Rules run by `swiftlint analyze` (experimental)
  - explicit_self

# configurable rules can be customized from this configuration file
# binary rules can set their severity level
force_cast: warning # implicitly
force_try:
  severity: warning # explicitly
# rules that have both warning and error levels, can set just the warning level
# implicitly
line_length: 110
# they can set both implicitly with an array
type_body_length:
  - 300 # warning
  - 400 # error
# or they can set both explicitly
file_length:
  warning: 500
  error: 1200
# naming rules can set warnings/errors for min_length and max_length
# additionally they can set excluded names
type_name:
  min_length: 4 # only warning
  max_length: # warning and error
    warning: 40
    error: 50
  excluded: iPhone # excluded via string
identifier_name:
  min_length: # only min_length
    error: 4 # only error
  excluded: # excluded via string array
    - id
    - URL
    - GlobalAPIKey
reporter: "xcode" # reporter type (xcode, json, csv, checkstyle, junit, html, emoji, sonarqube, markdown)

Reference: github.com/realm/SwiftLint#disable-rules-in-code