Objective-C: @class Directive before @interface?
What is the difference between these two class declarations? I don't understand why @class is utilized here. Thanks.
@class TestClass;
@interface TestClass: UIView {
UIImage *image1;
UIImage *image2;
}
and
@interface TestClass: UIView {
UIImage *image1;
UIImage *image2;
}
Solution 1:
@class
exists to break circular dependencies. Say you have classes A and B.
@interface A:NSObject
- (B*)calculateMyBNess;
@end
@interface B:NSObject
- (A*)calculateMyANess;
@end
Chicken; meet Egg. This can never compile because A's interface depends on B being defined and vice-versa.
Thus, it can be fixed by using @class
:
@class B;
@interface A:NSObject
- (B*)calculateMyBNess;
@end
@interface B:NSObject
- (A*)calculateMyANess;
@end
@class
effectively tells the compiler that such a class exists somewhere and, thus, pointers declared to point to instances of said class are perfectly valid. However, you couldn't call a method on an instance reference whose type is only defined as an @class
because there is no additional metadata available to the compiler (I can't remember if it reverts the call site to being evaluated as a call through id
or not).
In your example, the @class
is harmless, but entirely unnecessary.
Solution 2:
@class TestClass;
This merely declares "class TestClass will be defined".
In this case (the one you pasted) this has no effect of whatsoever, so these are the same.
However, in case you're going to define a protocol that would use your class name (as type of parameters passed to delegate, for example), you will need to declare @class TestClass
before the protocol definition, as your class is still not defined.
In general, if you need to mention your class name before the class definition is made, you will need to issue @class
declaration first
Solution 3:
As per Matt's answer there's absolutely no point to the @class
declaration in your code. @class
forward defines a class so that the compiler subsequently knows what general sort of unit you are referring to. Since Objective-C is pretty much typeless at runtime, that's often all that the compiler actually needs to know — just enough to distinguish the thing from an atomic C value.
I'm going to take a stab in the dark and say that because the instance variables are declared in the @interface
you're looking at some old code. Because it's old code, the @class
probably used to be somewhere else (eg, there was a delegate protocol declared in between) and has just ended up harmlessly stranded.