A very simple question about #import

Discussion in 'iOS Programming' started by mikezang, Nov 26, 2010.

  1. mikezang macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #1
    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;
    
     
  2. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
  3. mikezang thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #3
    That is very easy to understand. I will follow it.

    Thanks.
     
  4. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #4
    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?
     
  5. mikezang thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #5
    Why do you ask this? I think I had point which one is header and source:(
     
  6. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #6
    You're right, my first answer was correct and I still need more sleep.
     
  7. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #7
    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.
     
  8. bredell macrumors regular

    Joined:
    Mar 30, 2008
    Location:
    Uppsala, Sweden
    #8
    Shouldn't circular imports be impossible? I thought that the #import directive only included the file if it hadn't been included before.
     
  9. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #9
    You are correct!
     
  10. yaniv92648 macrumors member

    Joined:
    Oct 26, 2009
    #10
    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.
     
  11. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #11
    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."
     
  12. yaniv92648 macrumors member

    Joined:
    Oct 26, 2009
    #12
    Yes it is correct..

    But it is irrelevant to circular imports, circular imports can still occur!
     

Share This Page