Linter

Streamlining Xcode with tools and tips — Part 2

Harish Anbalagan

--

In Part-1, we made our code look nice. Now, let’s talk about something cool that can make our code even better.

Ever wish someone could tell you when your code isn’t quite right? Well, guess what? There’s a thing called a “linter” that does exactly that! It checks your code while you’re typing and gives you tips on how to make it better or if you’re breaking any rules. Xcode does some of this already, like warning us about big mistakes, but we can have even more control with a special tool.

Linters

Meet SwiftLint. It’s a helpful tool made by the Swift community. It’s like having a friend who knows a lot about good code and helps you write better.

To setup SwiftLint:

- Install using homebrew:

brew install swiftlint
  • Open any project you want the linter to work, then select the project in the file navigator, then select the primary app target, and go to Build Phases. Click the + and select “New Run Script Phase”. Insert the following as the script:
  • Expand the Run Script (which is added at the end) and add the below code in the box
if [[ "$(uname -m)" == arm64 ]]; then
export PATH="/opt/homebrew/bin:$PATH"
fi

if which swiftlint > /dev/null; then
swiftlint
else
echo "warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint"
fi
  • Hit Product -> Analyse in Xcode menu and 🎉 it’ll warn you about various violations and suggestions. You can customize it also, for more info check out the official guide here.
  • Adding SwiftLint is like having a code superhero by your side, making sure you write the best code possible. Stay tuned for more cool tips in our series!

--

--

Harish Anbalagan

Mobile dev passionate about building products. I have more projects ideas than the stars in the universe and unfinished project more than that.