I use several categories for various classes in Objective-C and I am wondering is to be considered best practice for where you import those categories? For instance, here’s a nice category for NSDate. I have two options:
-
I import this category only in the files I use it. I resist this only because I think that it will introduce clutter in my
#imports. I like them to be succinct and meaningful. -
I import this category in the prefix header and simply use it haphazardly throughout the application. I resist this because it feels global variable-ish. But then again, categories are not state, they’re just behavior.
I’m curious to hear how a larger team handles this in their coding standards and the specific reasons for their chosen standard.
Thanks in advance!
Import it where it is needed.
If it is needed in most of the files in your project, then putting it in the pch is not a terrible decision.
However, consider what Apple does with their frameworks. They provide individual header files for each part so you can include the parts you want. But, they also provide a conglomerate header for the entire framework where you can include everything in the framework with one import.
Now days, the compile times are so fast, that you don’t have to worry about being picky with includes as much as you used to (of course, you can debate whether this is a goo or bad thing).
I have settled to import complete Objective-C frameworks. One-off libraries like the data category will get included in the files they are needed, unless they are obviously going to be needed in a large percentage of files.
C++ files are still #included on a minimalistic as-needed basis.