Swiftlint static code analytics tool integration with Swift

A tool that checks your code with standardized swift language. It will check and enforce your code and shows the error for invalidate naming conversions.
 
In SwiftLint, we can use predefined rules or we can set our own custom rules in swift.
 

Installation Steps:

  • Open Terminal, fire brew install swiftlint               [One-time installation only]
  • Open your project in Xcode, go to Project -> Targets -> Add Run Script. Write below script: 

if which swiftlint >/dev/null; then

swiftlint lint –config “${SRCROOT}/.swiftlint.yml”

else

echo “warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint

fi

  • Rename Run Script with ‘Run SwiftLint’
  • Add .swiftlint.yml file in your project root directory.
  • Now Run/Build your project. Solve issue returned by SwiftLint.
 
We have to add this RunScript and .swiftlint.yml in each and every swift project that we are working on. 

Other Use:

  • Disable rules in code:
    • Rules we want to disable can be added to a  source file with a comment and ignored while compilation.
      • // swiftlint:disable <rule1> [<rule2> <rule3>…]
      • // swiftlint:disable identifier_name

 

swiftlint.yml file:

disabled_rules: # rule identifiers to exclude from running
- file_length
- function_body_length
- leading_whitespace
- line_length
- nesting
- operator_whitespace
- return_arrow_whitespace
- todo
- trailing_newline
- trailing_whitespace
- type_body_length
- type_name
# - identifier_name
# - variable_name
- comma
- statement_position
- generic_type_name
- force_cast
- shorthand_operator
- cyclomatic_complexity
- force_try
excluded: # paths to ignore during linting. overridden by `included`.
- Pods
- Debugging-SwiftLint-Configuration-YMLTests
- Debugging-SwiftLint-Configuration-YMLUITests
type_body_length:
- 300 # warning
- 400 # error
identifier_name:
min_length: # only min_length
error: 3 # only error
excluded: # excluded via string array
- id
- URL
- GlobalAPIKey
reporter: "xcode"
opt_in_rules:
- empty_count
- file_header
- explicit_init
- closure_spacing
- overridden_super_call
- redundant_nil_coalescing
- nimble_operator
- attributes
- operator_usage_whitespace
- closure_end_indentation
- first_where
- sorted_imports
- object_literal
- number_separator
- prohibited_super_call
- fatal_error_message
file_header:
required_pattern: |
\/\/
\/\/ .*?\.swift
\/\/ LetsNurture
\/\/
\/\/ Created by .*? on \d{1,2}\/\d{1,2}\/\d{2}\.
\/\/ Copyright © \d{4} LetsNurture\. All rights reserved\.
\/\/
line_length: 120

 

Want to work with us? We're hiring!