PDA

View Full Version : A matter of style ? or substance ?




mdeh
Oct 17, 2009, 08:11 PM
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
Oct 17, 2009, 11:23 PM
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
Oct 18, 2009, 12:35 AM
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.


MyClass.h
#import <Foundation/Foundation.h>
#import <Whatever/Whatever.h>
#import "ThisClass.h"

@interface MyClass : NSObject
@end

-=-=-=-=-

MyClass.m
#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
Oct 18, 2009, 03:16 AM
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
Oct 18, 2009, 02:19 PM
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.