I've read that the rule is to not #import
or #include
anything in .h
files. Is that really true, though?
Just today I've run into two separate occasions where I had to do it, one where I import a header file that contains a typedef enum
because my method stubs use it as parameters and another where I have to import a header file to declare delegatation.
What's the actual rule for these types of situations? Is it sometimes okay to do so – and if not, how can I do it differently?
There's no hard rule, but sometimes you do have to do it. You will also need to do if you inherit from something or need the protocol declaration.
In general I would restate the rule as "use forward declarations of @class and @protocol whenever possible."