I have used "swiftlint" in my iOS project. I also know to type "swiftlint autocorrect" at the project’s root path in the terminal to correct some warnings and errros.
Is there a way to run "swiftlint autocorrect" when I Run
the project in Xcode?
Edit
The command "swiftlint autocorrect" isn’t recommended using this way by the swiftlint
.
I found the way to use "swiftlint autocorrect" is to type it in the terminal.
Using "swiftlint autocorrect" when "Run" in the Xcode is a good practice or not?
4
Answers
The
swiftlint
project says that you can do this with the following steps:I think using
swiftlint
like this is recommended. The only warning iswhich is about the placement of the script and not the script itself. This says that
swiftlint
is only guaranteed to perform correctly on valid source code, so you are only guaranteed thatswiftlint
will work correctly if you place the script after the ‘Compile Sources’ phase so thatswiftlint
only runs once the compiler has verified your code.However, in my personal setup, I use
vim
to runswiftlint
when I save files (generally in an uncompilable state) which runs before compiling, and I have experienced no issues other than the occasional strange formatting when I haven’t yet closed all of the{
which I have opened. I do not think you will have any issues runningswiftlint
before the ‘Compile Sources’ phase.Note: the other warning on the page
is not about the ‘Run Script Phase’. This is referring to an Xcode plugin which will run
swiftlint
on save like is possible withvim
. To use this plugin on recent versions of Xcode, you need to disable the security feature SIP, which is not recommended. But again, this is unrelated to the ‘Run Script Phase’ solution.Just to expand slightly on the answer from @deaton.dg (which I found very useful).
The previous script I was replacing (in the Xcode Build Phases) had been contained in quotation marks
"
, however using the new script in quotation marks was causing a build error.It took a little while to work out it was the quotation marks that appeared to be causing issue.
So if you have installed SwiftLint via CocoaPods, and you wish to execute swiftlint autocorrect at build time, I found you need to make sure the script does not include quotation marks (ie. exactly as per the subsequent comment from @deaton.dg ):
Autocorrect is no longer available. If you run the autocorrect from the terminal, you should see the following message
So if you have installed swiftlint via CocoaPods, the script should look like
Yes, Please add the following script to
Build Phases
beforeCompile Resources