#TIL : XCode automatic format code on build time


28 Feb 2024 / by KhanhIceTea

XCode (the main IDE of Apple), but lacks much common features like auto formating code.
So we have to install a external tool (lol it's Apple tool itself) by brew install swift-format

Then I have to use a trick in Build Phases settings in XCode, we create a bash script run format tool on every build time (before source code has been built, because sometimes your code buidling will failed)

Here is the bash script, make sure the Run script below "Run Build Tool Plug-ins" step

run script

echo "Formating all source codes"
cd $SOURCE_ROOT
/opt/homebrew/bin/swift-format --ignore-unparsable-files -i -p -r .
echo "Formated all source codes."

Important : You have to turn off Build Options named User Script Sandboxing (this diffenrence of App Sandboxing), to allow your shell scripts can affected real filesystem instead of shadowing sandbox files, below is the screenshot

Turn of User Script Sandboxing


Sound good ?