Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mdeh

macrumors 6502
Original poster
Jan 3, 2009
345
2
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.

Only #import the super class in header files.
#import all classes you send messages to in implementation.
Forward declarations for everything else.

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

Thanks in advance.
 

HiRez

macrumors 603
Jan 6, 2004
6,250
2,576
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.
 

Darkroom

Guest
Dec 15, 2006
2,445
0
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.

Code:
[B]MyClass.h[/B]
#import <Foundation/Foundation.h>
#import <Whatever/Whatever.h>
#import "ThisClass.h"

@interface MyClass : NSObject
@end

-=-=-=-=-

[B]MyClass.m[/B]
#import "MyClass.h"

@implementation MyClass
@end

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?
 

Catfish_Man

macrumors 68030
Sep 13, 2001
2,579
2
Portland, OR
if so, what potential problems are there if an import occurs in a header rather than the implementation or vice-versa?

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.
 

mdeh

macrumors 6502
Original poster
Jan 3, 2009
345
2
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.


Thanks to all who answered. I will start following the advice suggested here.
Thanks.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.