SwiftLint in Depth | Kodeco

Learn how to use and configure SwiftLint in detail, as well as how to create your own rules in SwiftLint for your project.


This is a companion discussion topic for the original entry at https://www.kodeco.com/38422105-swiftlint-in-depth

Hey guys, thanks for the article. Do you know if there is a way to add own-custom rule for a specific Swift type (e.g. Double/Bool). Something like “Don’t use .description with Decimal type”? Thanks for answer

Hello @casperxdd,
I honestly have no idea how this can be possible with SwiftLint, I’m quite sure that regex based rules won’t help with this use-case. I’ll try to investigate a little perhaps I find a way for that, Swiftlint or otherwise.

1 Like

In the version of Xcode I’m using (14.3) there is no Workspace Settings on the File Menu. It seems to have been changed to Project Settings…

That’s what I’ve used to change the settings for the DerivedData

Following on from my earlier comment, I have successfully installed SwiftLint using Homebrew. However when I try to run it in Terminal I get a message:

SourceKittenFramework/library_wrapper.swift:31: Fatal error: Loading sourcekitdInProc.framework/Versions/A/sourcekitdInProc failed
zsh: trace trap swiftlint

I’m no expert in Terminal but I’m on an M1 Mac and I’ve seen comments to the effect that SwiftLint is installed in “/opt/homebrew/bin/swiftlint” rather than “/usr/local/bin/” so maybe the SwiftLint script in the starter program needs altering?

I’ve no idea how to do that . . .

David

Hello @drparker48,
For the first message, Xcode will change the option from the menu and the name of the window if you opened the project file or the workspace file. In either case it shouldn’t make a difference.
During the whole tutorial I used the workspace file.
For the second point, This is 99.9% a local issue. Please double check that there is a proper commandline version selected from Xcode preferences:

If there was a problem with the installation location of SwiftLint, it would have given you a different error message like command not found

Thanks for the response - my Xcode Locations seems to be correct so I must have gone wrong in the setup somewhere. I’ll go back and start again!

The command line tools says “(no xcode selected)” under the drop-down.
Open the drop-down and select xcode 14.3 again
I suspect that’s the problem you were facing
I know it’s confusing and not easy to spot the first time :smiley:

Thank you!

That fixed things and I successfully ran SwiftLint on the Starter project.

It might be worth adding what you advised into your tutorial in case others fall into the same trap.

Thanks again!

David

Thank you for a great tutorial. One question, though: I want to have rules to report unused_import and unused_declaration, but they never seem to trigger. I copied the analyser rules from the rules that are used by Kodeco and then added an unused import to one of my files, but no reports. I tried from Xcode and from the command line.
Any ideas of what I might be doing wrong?

Analyzer rules work a little differently: GitHub - realm/SwiftLint: A tool to enforce Swift style and conventions.
They are not used with the normal lint command.

I honestly didn’t play around much with this command but its definitely worth checking out from their docs.

Hi, What’s the proper way to integrate the SwiftLint with a Swift Package?
So it can scan the package code