Monday, November 19, 2012

objective c @class

In Objective C @class is used for forward declaration. It tells the compiler that there’s a class of that name. we can use it in the interface declarations.

Lets see the sample code below.
@interface example : NSObject
{
    //compiler dont know where this class located
    forwardDemo *obj;
}
@end

@interface forwardDemo : NSObject
{
    //data
}
@end


In the above sample code, we have a interface  example, data variable to that interface is object of another interface forwardDemo.  But example interface dont know the declaration/existance of the forwardDemo interface, this is because example is declared before forwardDemo interface. To overcome this problem, we need to use the forward declaration. In objective C which is achieved by using @class keyword. Solution for the above is given below.
//forward declaration
@class forwardDemo;
@interface example : NSObject
{
    forwardDemo *obj;
}
@end

@interface forwardDemo : NSObject
{
    //data
}
@end


Popular Posts