How to avoid chaos in a collaborative Xcode project

Nahuel Zapata
Aug 31, 2018 · 5 min read

What’s it all about?

Before digging deeper, let’s go over a few concepts.


Installation

You have many ways to install SwiftLint on your mac, but the most common and simple one is by using Brew:

brew install swiftlint

Xcode and project configuration

Now that we accomplished the installation, it’s necessary to configure our project to run SwiftLint. In order to allow Xcode to run swiftlint command, you have to create a run script for the target.

if which swiftlint >/dev/null; then
swiftlint
else
echo “Warning: SwiftLint is not installed, download it from https://github.com/realm/SwiftLint"
fi
The build phase should look like this.

Warning!

Let me tell you in advance, that in case you use any dependency manager, you will also have errors from the 3rd party libraries we integrated. And this is when you might think “Hell, I don’t need this. I can live without a linter”, but don’t lose your faith just yet!

.swiftlint.yml
  • opt_in_rules: SwiftLint has a set of default rules, that runs without configuring anything. But it also has some optional rules, that, by the way, are very interesting, but you have to specify on this file that you want those rules to run on every build command.
  • excluded: This key excludes the specified folders, files, etc. But maybe the thing that you may want here is the dependency manager, just because almost all the dependencies aren’t aligned with SwiftLint rules.
  • The other lines just configure particular rules. For example the force_cast one is supposed to trigger a warning instead of an error, or the identifier_name key that works for the min and max values.

SwiftLint hacks

Sometimes the next magic command will be your best friend. It will help you a lot to avoid repetitive fixing fool errors like trailing or sorting the imports.

swiftlint autocorrect

Xcode text editing help

While using Xcode as your IDE, you have the possibility to configure it to help you.



About SwiftLint

SwiftLint is maintained and funded by Realm Inc. But its code is open source, you can contribute to the project with new rules or bug fixing if you want. You can learn more about SwiftLint by taking a look at this video.

SwiftLint and CI integration

SwiftLint has an integration with Fastlane, another great tool for iOS development and for automating all your deployment processes. But if you have a CI server and no Fastlane configured, you can also integrate SwiftLint if your CI server happens to be Jenkins, Travis or Circle.


Conclusion

In my opinion, SwiftLint is a great tool for iOS/macOS development, that really helps us to keep our codebase very clean, maintainable and aligned with both our team and the Swift Community.

For Where Clause

intive Developers

At intive we’re building great digital products for our customers. Day by day. We want to share with you our way of doing things, the challenges we face, the tricks and shortcuts we discover. A little peek behind the scenes — welcome to our intive_dev blog!

Thanks to Fernando Lescano and Mischa Hildebrand.

Nahuel Zapata

Written by

iOS Software Engineer @ Despegar.com | Passionate for Swift language and Software Engineering | #GoVegan

intive Developers

At intive we’re building great digital products for our customers. Day by day. We want to share with you our way of doing things, the challenges we face, the tricks and shortcuts we discover. A little peek behind the scenes — welcome to our intive_dev blog!