Periphery addresses a common challenge faced by developers: maintaining clean, efficient codebases. Periphery, an open-source tool, offers a powerful solution for identifying and eliminating unused code in Swift projects, which can significantly improve app performance, reduce compile times, and enhance overall code quality.

In this post we will explain how to setup Periphery on your iOS project for later on force any code situations to trgger expected warnings.

Setup Periphery

First step is install periphery by using homebrew installation method:

brew install periphery

Next step is creating juts a regular iOS Sample app with XCode: 

brew install periphery

Move to the folder where the project was created and type:

And setup periphery for current iOS project:

periphery scan --setup

This will generate a hidden configuration file.

Get back to XCode Project:

And select the target, Build Phases, add (+) and NewScriptPhase. Do not forget unchecking ‘Based on dependency analysis’ option.

Script basically consists in calling periphery for performing and scan. Press CMD+B for build and check if setup was ok:

For fixing this issue just set User Script Sandboxing to No on Build Settings:

Turn to build again for checking that now thi time all works fine:

Detecting dead code

Periphery aims to detect and report unused declarations in your code. These declarations include classes, structs, protocols, functions, properties, constructors, enums, typealiases, and associated types. As expected, Periphery can identify straightforward cases, such as a class that is no longer referenced anywhere in your codebase. For the purpose of this post we have added some dead code for checking that warnings are really trigger every time we build de code (CMD+B).

Now appears new warnings that aims to dead code, but appart from thead code this utility also allows to detectct unused parameters:

Conclusions

Periphery is one of those tools that its setup effort is very low and will help you on removing dead code. If you’re interested in exploring the implementation further, you can find the source code used for this post in the following repository

References

Copyright © 2024-2025 JaviOS. All rights reserved