@class在Objective-C中做什么? [英] What does @class do in Objective-C?
问题描述
任何人都可以向我解释@class
声明在Objective-C中的作用以及在什么情况下应使用此声明?
Can anyone please explain me what does @class
declaration do in Objective-C and what are the cases in which we should use this declaration?
推荐答案
这是一个前向声明.从本质上讲,它告诉编译器有一个该名称的类.我在接口声明中使用它:
It’s a forward declaration. It essentially tells the compiler that there’s a class of that name. I use it in the interface declarations:
@class Foo;
@interface Bar : NSObject {
Foo *someFoo;
}
@end
当然,您可以改为导入Foo
的标头:
Of course you could import the header for Foo
instead:
#import "Foo.h"
@interface Bar : NSObject {
Foo *someFoo;
}
@end
但是,如果someFoo
不会向Bar
的用户公开,则他们将导入一个多余的头文件,这对他们没有用.使用@class
声明,Bar
的用户不会看到额外的导入,因为Foo.h
将被导入到Bar
的实现文件中.
But if someFoo
is not exposed to users of Bar
, they would import an extra header file that’s of no use to them. With the @class
declaration the users of Bar
see no extra import, because Foo.h
will be imported in the implementation file of Bar
.
这篇关于@class在Objective-C中做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!