A matter of style ? or substance ?

Discussion in 'Mac Programming' started by mdeh, Oct 17, 2009.

  1. mdeh macrumors 6502

    Jan 3, 2009
    Hi All,
    Up to now, I have always imported classes into the header file, but noticed that in Apple's Sketch program, the programmer imports them into the implementation file.
    Also, found this on StackOverflow.

    Is this a matter of substance or style ? :) Or, is this Holy-War territory?

    Thanks in advance.
  2. HiRez macrumors 603


    Jan 6, 2004
    Western US
    Yes, you will run into problems if you try to #import everything in your .h files, not for everything but it will happen. And some of these errors can be cryptic and hard to figure out exactly what the problem is. I use @class for everything I can in the interface file.
  3. Darkroom Guest


    Dec 15, 2006
    Montréal, Canada
    i've always wondered about this also, especially since i am under the impression that any import of a header file to the implimentation file will also import the header file's imports.

    #import <Foundation/Foundation.h>
    #import <Whatever/Whatever.h>
    #import "ThisClass.h"
    @interface MyClass : NSObject
    #import "MyClass.h"
    @implementation MyClass
    so in the code above, since the implimentation file has imported MyClass.h, doesn't it also take <Whatever/Whatever.h> and "ThisClass.h" with it, since it's apart of the header file?

    if so, what potential problems are there if an import occurs in a header rather than the implementation or vice-versa?
  4. Catfish_Man macrumors 68030


    Sep 13, 2001
    Portland, OR
    There are a few issues. The primary one is circular imports. a.h imports b.h, b.h imports c.h, and c.h imports a.h. That's safe if you forward-declare with @class, @protocol, etc..., but won't compile properly if you import.

    Secondarily, it can speed up compiles.
  5. mdeh thread starter macrumors 6502

    Jan 3, 2009

    Thanks to all who answered. I will start following the advice suggested here.

Share This Page