How to easily find unused Swift code in Xcode
Stay sane and remove unused code in Xcode easily with this ruby script. Add the script in a ruby file in your projects main folder called unused.rb. You can either run the code in the terminal with:
$ ruby unused.rb
The terminal will spit out a list of unused functions and variables found in your project.
If you’d like to have it be easily displayed in Xcode, navigate to your projects run scripts under Build Phases. Add the following shell script to run unused script automatically:
if [ -f "$file" ]
echo "$file found."
ruby unused.rb xcode
echo "unused.rb doesn't exist"
Afterwards run your project and the unused code warnings should be visible in the build time warnings pane.
Keep in mind this code will have a lot of false positives (protocols, functions, and system delegates) and false negatives, so be careful when deleting any “unused” delegate methods like collectionView’s numberOfSectionsInCollectionView.
If you don’t want to bother your coworkers with this script warning output, add the unused.rb file to your projects .gitignore (the run script above checks for its existence before running it).
Credit to Paul Taykalo for his really useful script.