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
939
41
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;
 
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?
 
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:(
 
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.
 
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.
 
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."
 
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.