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

mikezang

macrumors 6502a
Original poster
May 22, 2010
854
7
Tokyo, Japan
I remember all Objective-C guides and samples show me to use @class CLASS_NAME in header file, then use #import CLASS_HEADER.

I just want to know why not use #import CLASS_HEADER in header file directly, so that you don't need to modify two files.

Code:
// xxx.h
@class DetailTableViewController;

@interface MasterTableViewController : UITableViewController {
    DetailTableViewController *detailTableViewController;
}

// xxx.m
#import "DetailTableViewController.h"

@implementation MasterTableViewController

@synthesize detailTableViewController;

Code:
// xxx.h
#import "DetailTableViewController.h"

@interface MasterTableViewController : UITableViewController {
    DetailTableViewController *detailTableViewController;
}

// xxx.m
@implementation MasterTableViewController

@synthesize detailTableViewController;
 

lloyddean

macrumors 65816
May 10, 2009
1,047
19
Des Moines, WA
Actually looking at your first post now that I'm awake I'll revise that following a query.

Your first fragment above - is that from a header or an implementation file?
 

mikezang

macrumors 6502a
Original poster
May 22, 2010
854
7
Tokyo, Japan
Actually looking at your first post now that I'm awake I'll revise that following a query.

Your first fragment above - is that from a header or an implementation file?
Why do you ask this? I think I had point which one is header and source:(
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
Also reduces likelihood of circular imports.

Not everyone agrees with this reasoning. Some prefer using the imports in the headers because it results in fewer lines of code.

I prefer to use the forward declarations wherever possible and imports wherever required.
 

yaniv92648

macrumors member
Oct 26, 2009
96
0
Yes but it's irrelevant to circular imports

Because if Class A tries to import Class B and Class B tries to import Class A
so assuming Class A tries to import first then the compiler tries to fully compile Class B but that means that it has to return to Class A and fully compile it in order for Class B to be fully compiled and so on...
this is Circular Import.
 

lloyddean

macrumors 65816
May 10, 2009
1,047
19
Des Moines, WA
From Apples title "The Objective-C Programming Language, Tools & Languages: Objective-C" Copyright 2009, released 2009-10-19.

Concerning '#import": "This directive is identical to #include, except that it makes sure that the same file is never included more than once."
 

yaniv92648

macrumors member
Oct 26, 2009
96
0
Yes it is correct..

But it is irrelevant to circular imports, circular imports can still occur!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.