Welcome, Guest. Please login or register.
Did you miss your activation email?
July 25, 2016, 07:37:34 PM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan

+ Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+ Old Stuff
| |-+ Chapter Study
| | |-+ Chapter 8 - Inheritance
| | | |-+ Difference between @class directive and defining class through .h file
Pages: [1] Go Down
Author Topic: Difference between @class directive and defining class through .h file (Read 2088 times)
Posts: 8

on: September 24, 2010, 02:00:10 PM

Hi Steve,
First sorry for my english.
I am little stumped on this chapter, because use of @class directive in real life is not crystal clear. As I could understand you, no need for it at all, because we can do it all through defining new class XYPoint (including all variables in that class). This is my version (sorry, it is on croatian), and please be patient to look at it, and please help me to understand what is difference by including @class directive in another .h file, and defining separate class.
This is the code:

Code: (Objective-C)
#import <Foundation/Foundation.h>

@interface XYPoint : NSObject
int x;
int y;

@property int x, y;

-(void) setX: (int) xKoordinata setY: (int) yKoordinata;


Code: (Objective-C)
#import "XYPoint.h"

@implementation XYPoint

@synthesize x, y;

-(void) setX: (int) xKoordinata setY: (int) yKoordinata
x = xKoordinata;
y = yKoordinata;


Code: (Objective-C)
#import <Foundation/Foundation.h>

@interface Pravokutnik : NSObject
int sirina;
int visina;

@property int sirina, visina;

-(int) povrsina;
-(int) opseg;
-(void) setSirina:(int) s setVisina: (int) v;


Code: (Objective-C)
#import "Pravokutnik.h"

@implementation Pravokutnik

@synthesize sirina, visina;

-(int) povrsina
return (sirina + visina) * 2;

-(int) opseg
return sirina * visina;

-(void) setSirina:(int) s setVisina: (int) v
sirina = s;
visina = v;


Code: (Objective-C)
#import <Foundation/Foundation.h>
#import "Pravokutnik.h"
#import "XYPoint.h"

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Pravokutnik *mojPravokutnik = [[Pravokutnik alloc] init];
XYPoint *mojaLokacija = [[XYPoint alloc] init];

[mojaLokacija setX:100 setY:200];
[mojPravokutnik setSirina:5 setVisina:8];

NSLog (@"Sirina = %i, Visina = %i", mojPravokutnik.sirina, mojPravokutnik.visina);
NSLog (@"Povrsina = %i, Opseg = %i", mojPravokutnik.povrsina, mojPravokutnik.opseg);
NSLog (@"Lokacija na %i, %i", mojaLokacija.x, mojaLokacija.y);

[mojaLokacija release];
[mojPravokutnik release];
    [pool drain];
    return 0;

Don't give up!

Hero Member
Posts: 3114

Reply #1 on: September 24, 2010, 03:17:14 PM

The @class directive doesn't replace the need to define the class.  It only obviates the need to import the corresponding .h file when the compiler doesn't need to know any more information about a class.   For example, in this interface section:

Code: (Objective-C)
@class XYPoint;

@interface Rectangle : NSObject
    int  width;
    int  height;
    XYPoint  *origin;

The compiler is just happy to know that XYPoint is the name of a class.  However, in this example:

Code: (Objective-C)
origin.x = 100;

The @class directive wouldn't give the compiler enough information.  The compiler doesn't know that the XYPoint object has a setter method called setX:.   You have to import the XYPoint.h header file for it to know that.

So again, the bottom line is that if you use a method from a class, you need to import the header file.  If you just  reference the class name, then the @class suffices to tell the compiler all it needs to know.


Pages: [1] Go Up
Jump to:

Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.