Objective-C:前向类声明
我正在编写一个多视图应用程序,它利用一个名为 RootViewController 的类在视图之间切换。
在我的 MyAppDelegate
标头中,我创建了一个名为 rootViewController
的 RootViewController
实例。我见过这样的例子,其中 @class 指令被用作“前向类声明”,但我不太确定这意味着什么或完成什么。
#import <UIKit/UIKit.h>
@class RootViewController;
@interface MyAppDelegate
.
.
.
I'm writing a multiview app that utilizes a class called RootViewController
to switch between views.
In my MyAppDelegate
header, I create an instance of the RootViewController
called rootViewController
. I've seen examples of such where the @class directive is used as a "forward class declaration," but I'm not quite sure what this means or accomplishes.
#import <UIKit/UIKit.h>
@class RootViewController;
@interface MyAppDelegate
.
.
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它基本上告诉编译器类 RootViewController 存在,但没有指定它到底是什么样子(即:它的方法、属性等)。您可以使用它来编写包含
RootViewController
成员变量的代码,而无需包含完整的类声明。这在解决循环依赖关系时特别有用 - 例如,其中
ClassA
有一个ClassB*
类型的成员,而ClassB
有一个类型的成员类型ClassA*
。您需要先声明ClassB
,然后才能在ClassA
中使用它,但也需要先声明ClassA
,然后才能在中使用它B 类。前向声明允许您通过告诉
ClassA
存在ClassB
来克服这个问题,而不必实际指定ClassB
的完整规范。您往往会发现大量前向声明的另一个原因是有些人采用前向声明类的约定,除非他们绝对必须包含完整的声明。我不完全记得了,但这可能是 Apple 在其 Objective-C 指导风格指南中推荐的内容。
继续上面的示例,如果您的
ClassA
和ClassB
声明位于文件ClassA.h
和ClassB.h
中> 分别,您需要#import
来在另一个类中使用其声明。使用前向声明意味着您不需要#import
,这使得代码更漂亮(特别是当您开始收集相当多的类时,每个类在使用时都需要一个 `#import),并通过最大限度地减少编译器在编译任何给定文件时需要考虑的代码量来提高编译性能。顺便说一句,虽然问题只涉及 Objective-C 中的前向声明,但所有前面的注释也同样适用于 C 和 C++(可能还有许多其他语言)中的编码,它们也支持前向声明,并且通常将其用于相同的目的。
It basically tells the compiler that the class
RootViewController
exists, without specifying what exactly it looks like (ie: its methods, properties, etc). You can use this to write code that includesRootViewController
member variables without having to include the full class declaration.This is particularly useful in resolving circular dependencies - for example, where say
ClassA
has a member of typeClassB*
, andClassB
has a member of typeClassA*
. You need to haveClassB
declared before you can use it inClassA
, but you also needClassA
declared before you can use it inClassB
. Forward declarations allow you to overcome this by saying toClassA
thatClassB
exists, without having to actually specifyClassB's
complete specification.Another reason you tend to find lots of forward declarations is some people adopt a convention of forward declaring classes unless they absolutely must include the full declaration. I don't entirely recall, but possibly that's something that Apple recommends in it's Objective-C guiding style guidlines.
Continuing my above example, if your declarations of
ClassA
andClassB
are in the filesClassA.h
andClassB.h
respectively, you'd need to#import
whichever one to use its declaration in the other class. Using forward declaration means you don't need the#import
, which makes the code prettier (particularly once you start collecting quite a few classes, each of which would need an `#import where it's used), and increases compiling performance by minimising the amount of code the compiler needs to consider while compiling any given file.As an aside, although the question is concerned solely with forward declarations in Objective-C, all the proceeding comments also apply equally to coding in C and C++ (and probably many other languages), which also support forward declaration and typically use it for the same purposes.
前向声明主要是为了避免循环导入,其中一个文件导入另一个文件,该文件导入第一个文件等。基本上,当您导入文件时,文件的内容会在构建项目时在导入点被替换,然后将其馈送给编译器。如果你有循环导入,你就会有一个永远无法编译的无限循环。幸运的是 xcode 会在尝试之前告诉你这一点。前向声明表示“不要导入此类,而只需知道它存在。”如果没有导入或前向声明,您会收到一条错误消息,表明不存在此类。
Forward declarations are mainly to avoid circular imports, where one file imports another file which imports the first file etc. Basically when you import a file, contents of the file are substituted at the point of import when you build your project, which is then fed to the compiler. If you have circular imports, you'd have an infinite loop which would never compile. Fortunately xcode will tell you about this before trying. The forward declaration says "Don't import this class but just know that it exists. " Without either an import or a forward declaration, you get an error that no such class exists.
@class
或前向类声明
(不完整类型) - 只需告诉编译器该类存在。在这种情况下,编译器不知道有关类型内存布局的任何信息 - 类大小、成员或方法。这就是为什么您只能用于通过引用和指针定义类。优点:
@class
orforward class declaration
(incomplete type) - just tell to a compiler that this class exists. In this case the compiler does not know anything about type memory layout - class size, members, or methods. That is why you can only use for defining classes via references and pointers.Advantages: