这两个类声明之间有什么区别?我不明白这里为什么使用@class.谢谢
@class TestClass;
@interface TestClass: UIView {
UIImage *image1;
UIImage *image2;
}
和
@interface TestClass: UIView {
UIImage *image1;
UIImage *image2;
}
这两个类声明之间有什么区别?我不明白这里为什么使用@class.谢谢
@class TestClass;
@interface TestClass: UIView {
UIImage *image1;
UIImage *image2;
}
和
@interface TestClass: UIView {
UIImage *image1;
UIImage *image2;
}
@class
的存在打破了循环依赖.假设你有A班和B班.
@interface A:NSObject
- (B*)calculateMyBNess;
@end
@interface B:NSObject
- (A*)calculateMyANess;
@end
鸡见见蛋.这永远无法编译,因为A的接口依赖于B的定义,反之亦然.
因此,可以使用@class
:
@class B;
@interface A:NSObject
- (B*)calculateMyBNess;
@end
@interface B:NSObject
- (A*)calculateMyANess;
@end
@class
有效地告诉编译器这样一个类存在于某个地方,因此,声明指向所述类实例的指针是完全有效的.但是,您无法对类型仅定义为@class
的实例引用调用方法,因为编译器没有其他可用元数据(我不记得它是否将调用站点还原为通过id
进行的调用求值).
在你的例子中,@class
是无害的,但完全没有必要.