[Obj-c] Check imported categories exists in compile sources

Vlad Dugnist
ML-Works Engineering
2 min readMar 13, 2017

You may not know, but if you remove category file from target compile source, you’ll not get any errors. Instead of this, you’ll get crash at runtime.

Runtime crash caused by missing category in compile source

Here’s how you possibly get it:

  • Add new target for existing project (for push extension, imessage extension, etc.), import your category and start using it.
  • Create new category while having multiple targets and forget to turn on checkbox.
  • Add dependency file to your project that includes category and forget to add this category to compile source.

Few steps to reproduce the problem (you can download sample project here and start from paragraph 4):

  1. Create test project.
  2. Create NSObject category with some method.
  3. In AppDelegate create NSObject and call this category method.
  4. Run application and check that everything fine.
  5. Open Target->Build Phases->Compile Sources and remove NSObject category.
  6. Run application and check that it crashes at runtime without any error at compile time.

How can you fix it?

Get this gist and save it with the name check-categories-compiled.rb into project folder. Go to Target->Build Phases. Click on + -> New run script phase and type ruby check-categories-compiled.rb

Xcode build phases screenshot

After that you’ll get compile time error if something went wrong:

Xcode compile time error example

--

--