Integrate SwiftLint into your project-Swift
SwiftLint helps you to follow the style guide rules that are accepted by the Swift community. Follow the following steps to integrate the SwiftLint in your project when you are going to start your project so that you can do it from the starting point. But if you missed that no worries, Integrate it now and take care next time. This will help you more during your development.
First, add the pod in your pod file(If you are going to use pods the first time please create a pod file for your project, and then you can move further).
After this, You need to save and run the command terminal on the project pod file path.
Once you are done with that, Now you need to create a swiftlint.yml file, For this again you need to run the command on the same path where you run the pod install.
This command will create a file and this file will be hidden on the same path if you want to check it. Go to the project folder path as you follow on the terminal and press shift+command+. (shift+command+dot)
The hidden files you can check there.
Now, Run the following command on the terminal to open the swiftlint.yml file so that we can add our rule for SwiftLint.
Once you run this command swiftlint.yml file will open and you can add your rules to this file and save it.
# ******* Add Rules as per your company Guidelines *******
disabled_rules: # rule identifiers to exclude from running
opt_in_rules: # some rules are only opt-in
# 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`.
- Source/*/ExcludedFile.swift # Exclude files with a wildcard
analyzer_rules: # Rules run by `swiftlint analyze` (experimental)
# configurable rules can be customized from this configuration file
# binary rules can set their severity level
force_cast: error # implicitly
severity: warning # explicitly
# rules that have both warning and error levels, can set just the warning level
# they can set both implicitly with an array
warning: 300 # warning
error: 500 # error
# or they can set both explicitly
# naming rules can set warnings/errors for min_length and max_length
# additionally they can set excluded names
min_length: 3 # only warning
max_length: # warning and error
excluded: iPhone # excluded via string
#allowed_symbols: [“_”] # these are allowed in type names
#here you can add a file, or folder you want to exclude for this toll
reporter: “Xcode” # reporter type (xcode, json, csv, checkstyle, junit, html, emoji, sonarqube, markdown)
You can add or remove the rule from above or from the swiftlint.yml file as per your company/project guidelines.
Lastly, You need to add the script to your target, Go to your target, select build phase and click on the + icon. Select New Run Script Phase and give the name as per your choice and add the following command there.
Compile your code you will see the warning and do whatever needs to be done for this warning.