Swiftlint and iOS Swift Projects

Why swiftlint should be integrated in swift based ios projects

Swiftlint and iOS Swift Projects

Why swiftlint should be integrated in swift based ios projects

As swift becomes so popular between iOS app developer’s, its time to follow Swift style and conventions. In this article, I am going to explain how to help your team member’s as well as yourself by integrating swiftlint.

SwiftLint

A tool to enforce Swift style and conventions, which covers most of the items and some of them are depicted below:
1. Whitespace
2. Prefer let-bindings over var-bindings wherever possible
3. Return and break early
4. Avoid Using Force-Unwrapping of Optionals
5. Avoid Using Implicitly Unwrapped Optionals
6. Prefer implicit getters on read-only properties and subscripts
7. Always specify access control explicitly for top-level definitions
8. When specifying a type, always associate the colon with the identifier
9. Only explicitly refer to self when required
10. Prefer structs over classes
11. Make classes final by default
12. Omit type parameters where possible
13. Use whitespace around operator definitions

SwiftLint hooks into Clang and SourceKit to use the AST representation of your source files for more accurate results.

No more theory, let’s start integrating into your swift project.

Installation

Install Homebrew

/usr/bin/ruby -e “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Using Homebrew:

brew install swiftlint

Xcode

Integrate SwiftLint into an Xcode scheme to get warnings and errors displayed in the IDE. Just add a new “Run Script Phase” with:

if which swiftlint >/dev/null; then
swiftlint
else
echo "warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint"
fi

Alternatively, if you’ve installed SwiftLint via CocoaPods the script should look like this:

"${PODS_ROOT}/SwiftLint/swiftlint"

Once you are done with the integration of swift lint with your iOS swift project, its time to rock!!!!! Lets run your project and see..

Swiftlint warnings and error

Swiftlint rules

Its seems difficult to understand the below image but to get actual understanding and use case , its better to check with your project.

swiftlint rules

I hope you will try with your project and see if its helps you, please provide inputs incase any modification required, Hope you like it!