Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > Mac OS X Development Forums > Objective-C, Python, Ruby Development

Reply
 
LinkBack Thread Tools Display Modes
Old 03-02-2010, 07:38 AM   #1 (permalink)
Registered Member
 
Join Date: Dec 2009
Posts: 97
Default @class or header-import?

Since I come from a Java background I am still kind of confused with the use of the @class command.

In which situations should I use @class and in which should I just #import the header file of the class!?

Thanks
SteveMobs is offline   Reply With Quote
Old 03-02-2010, 02:37 PM   #2 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Default

You must use @class when you have a header loop. If Parent.h includes Child.h and Child.h includes Parent.h then you have a header loop, and you will not be able to compile. In that situation you can break the loop by doing this:

Parent.h includes Child.h
Child.h has @class line (forward declaration) for Parent class
Child.m includes Parent.h (required to know the methods and properties of Parent)
__________________

Free Games!
smasher is offline   Reply With Quote
Old 03-02-2010, 03:52 PM   #3 (permalink)
Pro. Game Developer
iPhone Dev SDK Supporter
 
Join Date: Feb 2009
Location: żLa Islas Hermosas?
Posts: 2,178
Default

Additionally, a class forward declaration can actually be used in lieu of the full header inclusion wherever the reference(s) to the class is/are only to the "name" of the class and not the class' functionality.

So, if I'm writing a header for a new class "NewClass" that has a member of class "A", I can forward declare class "A" rather than including its full header:
Code:
@class A; // forward declare class A
@interface NewClass : NSObject
{
@private
    A *    m_myClassAMember; // this is a pointer, so once the compiler knows A is a class, it doesn't need to know anything else
}
@end
I can write my header this way in this instance because the compiler doesn't need to know what class "A" is capable of doing; it just needs to save the space for a reference to an instance of class "A", and that amount of space is constant regardless of the details of class "A".

Now, in the implementation you often have no choice but to include the header, because you now need to know what's in class "A", what its members are, what methods it can perform, etc., so your code looks something like:
Code:
#import "ClassA.h" // forward declaration is not adequate here
@implementation NewClass

- (void) someMethodInThisClass
{
    // can't do the next line without including ClassA's header, because forward declaration doesn't tell us about 'someClassAMethod'
    [m_myClassAMember someClassAMethod];
}

@end
Many software engineers consider it good programming practice to use a class forward declaration in lieu of header inclusion whenever possible as it theoretically reduces compile time, among other things.
__________________
~~ Word Flurry ~~ App Store / Website / Facebook
Kalimba is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 239
21 members and 218 guests
ADY, AragornSG, bookesp, BrianSlick, Dani77, Dattee, Dominus, dre, glenn_sayers, HemiMG, JasonR, karlam963, nobre84, Oral B, prchn4christ, Raggou, Rudy, spiderguy84, themathminister, viniciusdamone, vvenkatachallam
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,885
Threads: 89,229
Posts: 380,763
Top Poster: BrianSlick (7,129)
Welcome to our newest member, bookesp
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:20 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0