Writing beautiful code in Swift

Having well structured code makes development a lot more efficient and enjoyable. As a developer you can focus on the task at hand and let your creativity juices flow.

It’s easy to forget coding conventions, like a tracing newline for example, when you’re having a creative moment. This is where a linter comes into the picture. The purpose of a linter is to help you maintain your coding conventions.

If you use a linter you can focus on writing code. When you’ve finished you run the linter and follow the instructions to make sure you’re conforming to your coding conventions. Some linters can even fix a lot of your mistakes automatically.

So what if you want to add this to your existing xCode project? No problem. I added SwiftLint to the iOS App at Acast last week. I got 999+ warnings. There’s nothing wrong with this, if you don’t use a linter it’s difficult to follow coding conventions!

Since 999+ warnings was a bit overwhelming I decided to disable one rule at a time until I reached zero warnings. Once that was done I started enabling rules one by one. This made all the difference in the world, a seemingly impossible task suddenly became feasible!

How do you get this running on your project? It’s easy as pie and the instructions are found below. 👇🏽

  1. Install Brew by writing ruby -e “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" in your Terminal app
  2. Install Swiftlint by writing brew install swiftlint
  3. Open up your Xcode project
  4. Start by selecting your Project in the left bar. Then select the Target for which you want to add SwiftLint. Then choose Build Phases in the top right area of the window.
  5. You’re now ready to add a Build Phase to your selected Target. You’ll find the Add button above the Build Phases just a few pixels left. Click it and choose New Run Script Phase.
  6. Double click the new Build Phase and rename it to SwiftLint. (Not a must, but your project will be easier to maintain)
  7. Expand the Build Phase by clicking the triangle to the left of it.
  8. You should now see a black box that says Type a script or drag a script file from your workspace to insert its path. Replace it with:
if which swiftlint >/dev/null; then
swiftlint
else
echo "SwiftLint does not exist, download from https://github.com/realm/SwiftLint"
fi
This is how it looks like when you’ve successfully created the Build Phase

Voila, that’s it! Everytime you build your target SwiftLint will run and create warnings and errors to help you write better code. 🙌🏽

First time you fire up SwiftLint it will probably find tons of issues. To help you get started there’s an autocorrect that fixes some of the most simple issues. To run it go to your project folder in the Terminal app and write swiftlint autocorrect.

For more information, see the SwiftLint Github repo or browse the documentation by writing swiftline help in your Terminal.

Feel free to post comments if you have any thoughts or issues and I’ll try to help you out!