Categories
iOS macOS Swift Xcode

Adding SwiftLint to a project in Xcode

SwiftLint is a tool for validating the style and conventions in Swift files. It is easy to integrate to a project. Minimum set up consists of installing SwiftLint and adding a build phase to your app target in Xcode. SwiftLint comes with a default list of rules, but if you would like to change the rules then it is required to create a configuration file in yaml. Let’s take a look on how to install, add build step, and use a separate configuration file with a custom set of rules.

SwiftLint is available in homebrew so the easies way is to install it with brew install swiftlint command. Next step is to add the build phase to a target. I am gonna use one of my example app which is available on GitHub: SwiftPackageAppWorkspace. This project uses a workspace and includes an app project and Swift package.

git clone https://github.com/laevandus/SwiftPackageAppWorkspace.git
open ButtonGallery.xcworkspace
view raw homebrew.sh hosted with ❤ by GitHub
Cloning example project and opening it in Xcode.

Click on the ButtonGallery project in the file navigator, then on the iOS target and build phases. Will use the + button for adding a new run script phase. Note that we already use the config argument for letting SwiftLint know where the config file exists (by default SwiftLint looks for .swiftlint.yml file in the same folder the project file is).

if which swiftlint >/dev/null; then
# Relative path from the .xcodeproj which contains this script
swiftlint lint –config ../swiftlint.yml
else
echo "warning: SwiftLint not installed"
fi
view raw LintSources.sh hosted with ❤ by GitHub
Build phase calling swiftlint with custom configuration file in one folder up from the .xcodeproj.
Build phase which triggers swiftlint with custom configuration.

Last step is to add a custom configuration file to the repository checkout. We’ll add it to the checkout’s root folder which is the parent folder of the ButtonGallery.xcodeproj. I have went through the full list of rules available for SwiftLint and picked the ones which match with my coding style. SwiftLint has a list of default rules. The list of evaluated rules can be expanded with opt_in_rules and rules from default list can be disabled with disabled_rules list. Also I prefer to have else on a newline so I added statement_position configuration with statement_mode: uncuddled_else. Included defines a list of folder paths relative to the .xcodeproj calling swiftlint.

included:
../ButtonKit
../ButtonGallery
disabled_rules:
compiler_protocol_init
cyclomatic_complexity
file_length
force_cast
function_body_length
function_parameter_count
identifier_name
multiple_closures_with_trailing_closure
notification_center_detachment
line_length
trailing_whitespace
type_body_length
type_name
todo
opt_in_rules:
anyobject_protocol
array_init
closure_end_indentation
closure_spacing
collection_alignment
contains_over_filter_count
contains_over_filter_is_empty
contains_over_first_not_nil
contains_over_range_nil_comparison
convenience_type
discouraged_object_literal
discouraged_optional_boolean
empty_collection_literal
empty_count
empty_string
empty_xctest_method
enum_case_associated_values_count
expiring_todo
explicit_init
fallthrough
fatal_error_message
file_name_no_space
first_where
flatmap_over_map_reduce
identical_operands
joined_default_parameter
last_where
legacy_multiple
legacy_random
literal_expression_end_indentation
lower_acl_than_parent
# useful for frameworks with public interface – missing_docs
multiline_function_chains
multiline_parameters
multiline_parameters_brackets
nslocalizedstring_key
operator_usage_whitespace
optional_enum_case_matching
overridden_super_call
override_in_extension
pattern_matching_keywords
prefer_self_type_over_type_of_self
prefer_zero_over_explicit_init
private_outlet
prohibited_super_call
reduce_into
redundant_nil_coalescing
redundant_type_annotation
single_test_class
sorted_first_last
sorted_imports
static_operator
toggle_bool
unneeded_parentheses_in_closure_argument
unused_declaration
unused_import
vertical_parameter_alignment_on_call
vertical_whitespace_closing_braces
# if {
# }
# else {
# }
statement_position:
statement_mode: uncuddled_else
view raw swiftlint.yml hosted with ❤ by GitHub
Custom configuration file for SwiftLint.

The next time the target is built it will run SwiftLint with custom configuration and show warnings and/or errors in Xcode.

Warnings generated by SwiftLint in the example project.

Summary

SwiftLint is easy to set up and helps to keep the code style consistent in projects. In additional to style SwiftLint is capable of suggesting different coding conventions like use enum instead of struct with only static functions. For making it easy to add custom configuration to new project I have set up a command alias in ~/.zshrc which looks like this: alias xcode_lint_add='cp ~/Dev/swiftlint.yml swiftlint.yml && mate swiftlint.yml' Run xcode_lint_add in the root of the cloned project.

If this was helpful, please let me know on Mastodon@toomasvahter or Twitter @toomasvahter. Feel free to subscribe to RSS feed. Thank you for reading.

One reply on “Adding SwiftLint to a project in Xcode”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s