DEV Community

Cover image for Advanced iOS development - Build Phases Part 2
Omar Labib
Omar Labib

Posted on

Advanced iOS development - Build Phases Part 2

In part one, we were introduced to how run scripts can be utilized as part of Xcode build process with a trivial example..

In this post we will further demonstrate on run scripts but through very meaningful use case...

If you've ever worked with localizable files in iOS, I'm sure you have, You may have notice, I'm not sure you may, that Xcode doesn't detect duplicated keys in the files, i.e. you can have multiple keys with different or even same values and Xcode won't complain about this..

This of course is not a recommended practice at all, and must be avoided, and that's our duty in this post..

Not only that, but we're going to highlight duplicated keys in Xcode as follows..

Xcode error message

Insert the following code inside into the code block in your run phase step, this code can be found here..

Now let's go over it line by line...

  1. We find the Localizable.strings files in the directory of the project, and iterate over them.
  2. SRCROOT is an environment variable exposed by Xcode during the build process, thus FILE_PATH represents the absolute path to the Localizable.strings file.
  3. We extract only the keys from the file.
  4. We filter the keys to the only duplicated ones.
  5. We iterate over the duplicated keys to get the key and its occurrences in the file.
  6. We iterate over these duplicated occurrences and get the line number then we print what key is duplicated and at which line.

The cool part is when it comes to highlighting this error message in Xcode..
You just need to print the following message during the build process.

echo "${filePath}:${lineNumber}:${columnNumber}: error: {errorMessage}"
This highlights the message in Xcode as follows..

Xcode error message

If instead you want a warning instead of error in Xcode, you just need to change it to the following..
echo "${filePath}:${lineNumber}:${columnNumber}: warning: {errorMessage}"

This yields the following output..

Xcode warning message

Run scripts are such powerful features that can utilize to run any script you can think of... not only can you run shel scripts there, but you can run any sort of script, Your imagination is the limit 🚀

Top comments (0)