Fixing Xcode
Overview
Sometimes Xcode can get in an odd state. This guide is a progression of steps to reset things. After each step, test to see the problem is resolved.
Clean the Build
If the issue is building, the first step is to clean the build, which will force a clean build from scratch. Choose Product -> Clean Build Folder (or press Shift-Command-K). The standalone Clean command was removed when Xcode 10 made the new build system the default.
Reset the Simulator
Launch the iOS Simulator. From the Device menu, choose Erase All Content and Settings…, and click Erase to confirm. (In older Xcode this lived on the iOS Simulator menu as Reset Content and Settings.) To wipe every simulator at once from the terminal, run xcrun simctl erase all.
Delete Derived Data
Open Xcode -> Settings (or Preferences on macOS 12 and earlier) and select the Locations tab. Click the small arrow next to the Derived Data path to reveal the folder in Finder, then delete the folder for your project (or the entire DerivedData folder). The Organizer’s Projects tab — where this lived in older Xcode versions — has been removed.
You can also delete it directly from the terminal:
rm -rf ~/Library/Developer/Xcode/DerivedData
Xcode creates Derived Data when it indexes your files, among other things.
Delete the Module Cache
Within the same folder as your project’s Derived Data is a Module Cache. When Code Completion stopped working, deleting this fixed it.
Close Xcode and delete the ~/Library/Developer/Xcode/DerivedData/ModuleCache.noindex directory (modern Xcode appends .noindex to keep Spotlight from indexing the precompiled modules).
Delete Xcode Preferences
A long-shot next step is to reset Xcode’s preferences. In your terminal, run:
defaults delete com.apple.dt.Xcode