Skip to content

Instantly share code, notes, and snippets.

@shirishbankar1
Created February 25, 2020 07:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shirishbankar1/69f76e51408f1e46fcc3fa1416f7d30b to your computer and use it in GitHub Desktop.
Save shirishbankar1/69f76e51408f1e46fcc3fa1416f7d30b to your computer and use it in GitHub Desktop.
Swift - Lint Integration for iOS Projects
There are many ways of integrating Swift-Lint. I am going with the homebrew option.
First Install homebrew on your mac :
Open Terminal and enter below command :
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Once done enter below command :
brew install swiftlint
Done . SwiftLint is installed now. For using it in your project.
Open your project in Xcode goto Target -> Build phases -> Add Run script (Rename it since you can have multiple run scripts in future)
Add below code in your script editor :
if which swiftlint >/dev/null; then
swiftlint
else
echo "warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint"
fi
if you have not used homebrew & have used pods use below script :
${PODS_ROOT}/SwiftLint/swiftlint
There you are ready to use Siwft Lint.
Now build your project and you'll see errors and warning populating in Xcode issue navigator. No need to stress out.
These are the issues that are caused by the RULES defined in SwiftLint . You can choose to include/exclude/modify
certain rules by customising your own file for rules.
In Xcode click Files -> New -> File -> iOS tab -> Others section -> Empty File.
Name this file with extension .swiftlint.yml
Now since you have decided to use this file to overide the rules of SwiftLint. We need to let the Xcode know about it.
So we'll be changing the script we wrote in BuildPhases like below :
if [ "${CONFIGURATION}" = "Debug" ]; then
if which swiftlint >/dev/null; then
swiftlint --config "$SRCROOT/MyRulesFile.swiftlint.yml"
else
echo "warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint"
fi
fi
Done. Now you need to edit the yml file to adapt your rules.
We wont go deeped into editing this file as you can learn more about it here. https://github.com/realm/SwiftLint
If you want to disable a rule in particular file write below command somewhere at top of your file and all swiftLint will
ignore all error and warning in that file :
// swiftlint:disable all
and to acheive vice versa :
// swiftlint:enable all
It's also possible to modify a disable or enable command by appending :previous, :this or :next for only
applying the command to the previous, this (current) or next line respectively. Check below e.g.
// swiftlint:disable:next force_cast
let noWarning = NSNumber() as! Int
let hasWarning = NSNumber() as! Int
let noWarning2 = NSNumber() as! Int // swiftlint:disable:this force_cast
let noWarning3 = NSNumber() as! Int
// swiftlint:disable:previous force_cast
Now what if you only want particular rule/rules and ignore all others . We have below solution for that :
whitelist_rules: Acts as a whitelist, only the rules specified in this list will be enabled.
It cannot be specified alongside disabled_rules or opt_in_rules.
Usage :
whitelist_rules :
- force_try
So by this you code will only be verified againt force_try rule.
If you want to disable only few rules, use :
disabled_rules:
- colon
- comma
- control_statement
Putting above code in yml file will disable these 3 rules.
We can also choose to exclude particular folders/files to be verified against SwiftLint by using below :
excluded:
- Pods
- Source/ExcludedFolder
- Source/ExcludedFile.swift
For more deeper diving on editing rules tou can check : https://github.com/realm/SwiftLint
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment