Solve most of your Xcode problems through Cargo Cult

If you are an iOS or macOS developper, you probably work with Xcode. And if you work with Xcode, you've most probably had some weird times where, suddenly, things just stopped working. Here's how to solve that

Solve most of your Xcode problems through Cargo Cult

If you are an iOS or macOS developper, you probably work with Xcode. And if you work with Xcode, you've most probably had some weird times where, suddenly, things just stopped working.

The "index" would "prebuild" for hours, eating up your CPU.

Syntax highlighting and autocompletion would just abruptly be dropped, leaving you scratching your head in the middle of calling a method.

The project just would not build, because of some phantomatic build error.

Heck, sometimes, Xcode would just crash when opening a project! (and take it's time before displaying anything).

"Solving" this usually involves what some iOS developpers  (or, at least, I) call the "rain dance"! Thing is, this actually designates a whole group of different semi-"Cargo cultish" rituals that, more often than not, "solve" most of Xcode's issues.

Performed by Thomas/Ortiz Dance, the dance is called “Undamely”.  The picture was taken at Battery Dance festival in 2016.
Ready for the Rain Dance? - Photo by Tim Gouw / Unsplash

Personally, I've started to loose track of all the ways to "solve" Xcode's randomly occuring quirks. I've therefore decided to write them all down somewhere. And if I am to write them down, why not share it? So here we are!

Xcode's mysterious cargo cult rituals

Rain dance

  1. Clean project (Shift + ⌘ + k)
  2. Close Xcode
  3. Reopen Xcode
  4. Eventually clean the project again

This one is a classic, and it can solve many things, some of the time.

Voodoo ceremony

  1. Close Xcode
  2. Open your terminal
  3. Wipe out the Derived Data (rm -rf /Users/elfitz/Library/Developer/Xcode/DerivedData)
  4. Reopen Xcode

Pros: solves almost all problems
Cons: if you use Swift Packages, they'll be gone

Invoke the ancestors' spirits through the sacrifice of a pound of dandelions

  1. Close Xcode
  2. Open the project's folder.
  3. If you use a workspace, on the .xcworkspace file, show package content, delete xcuserdata.
  4. Repeat with .xcodeproj.
  5. Reopen Xcode.

Reference: https://stackoverflow.com/a/4456229/7243001

Can also be done via the command line (just cd into the .xcworkspace and .xcodeproj folders).

Banish Little Timmy back to whence he came

If you are using Xcode 12.x, you might be having issues with Xcode randomly hanging very often. It might be caused by Xcode being connected to your devices, either via USB or the network.

  1. Disable "Connect via network" for your devices
  2. If you have devices plugged in, unplug them.

Xcode will immediately stop hanging, albeit will sometimes crash ¯\_(ツ)_/¯

Reference: https://developer.apple.com/forums/thread/669378)

Summon the devil (may or may not cost you your soul)

Given the StackOverflow author's warning, I really wouldn't recommend it. But, you do you. It's just included here for reference, and in case I might need it some day.

Reference: https://stackoverflow.com/a/6247073/7243001

Reboot

A great, timeless, Classic. The only way to solve some cryptic permission issues that randomly pop up at build time and then won't go away.

The file couldn’t be saved.
Command PhaseScriptExecution failed with a nonzero exit code
The very helpful and self-explanatory Uncategorized error

Epilogue

And here you are. Xcode, an evil yet, sometimes, somehow, beautiful voodoo artefact from another eon has turned you into a pagan practicing unholy rituals, damning your soul for all eternity ¯\_(ツ)_/¯

Also, for those of you scratching their heads at the expression cargo cult, it's an actual thing that I definitively haven't made up.

Joking aside... I honestly may just make a Swift cli tool out of this.

Welcom to Hell! Thanks Xcode! - Photo by Peter Leong / Unsplash