• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag
 

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Where/when to #import
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Where/when to #import


  • Subject: Re: Where/when to #import
  • From: Nelson Santos <email@hidden>
  • Date: Tue, 9 Oct 2007 09:41:59 -0400

Hi Paul,

Here are the rules that I follow which seems to work very well for me:

(1) #import <Cocoa/Cocoa.h goes in your class's .h file.
(2) Use @class only in your .h files for all of your class references used (unless you derived your class from another class, in which you need to use the #import directive, because the compiler needs to know everything about the original class when you derive.)
(3) Place all other #import directives in your .m files.


I haven't encountered any problems yet using these rules. Hope that helps!
Nelson Santos




On Oct 9, 2007, at 9:09 AM, Paul Bruneau wrote:

Hi-

Could someone point me to where I learn all the proper rules about where and when to #import (and also @class)?

I have searched for this but haven't found it.

My history with importing is mostly to try to follow examples and/ or try things until they work.

Currently here are the rules in my head, which might not be correct:

1. put #import <Cocoa/Cocoa.h> in each class' .h file
2. put a @class in the .h file for each class about which the compiler complains
3. put #import "MyClass.h" in the MyClass.m file
4. put #import "MyOtherClass.h" in the MyClass.m file for each class about which the compiler complains



I had been using these rules which seemed to be cleaner to me but ran afoul of all the sample code I found, so I've been moving away from them:


1. put #import <Cocoa/Cocoa.h> in each class' .h file
2. put #import "MyClass.h" in the MyClass.m file
3. put #import "MyOtherClass.h" in the *MyClass.h* file for each class about which the compiler complains


This made sense to me because then each .h file knew about a lot more of my other classes and I didn't really ever have to use @class. And since my understanding of #import is that the compiler only imports each file once, there is no danger of redundant copying of headers. But as I said, this does not seem to be the correct way to do it.

Can anyone enlighten me? Thank you.
_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
40mac.com


This email sent to email@hidden

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Where/when to #import
      • From: Paul Bruneau <email@hidden>
    • Re: Where/when to #import
      • From: tiennou <email@hidden>
References: 
 >Where/when to #import (From: Paul Bruneau <email@hidden>)

  • Prev by Date: Binding NSMenuItem 'value' or 'title': Accessors not invoked
  • Next by Date: Binding NSMenuItem 'value' or 'title': Accessors not invoked
  • Previous by thread: Where/when to #import
  • Next by thread: Re: Where/when to #import
  • Index(es):
    • Date
    • Thread