Objective-C:前向类声明 [英] Objective-C: Forward Class Declaration
问题描述
我正在编写一个多视图应用程序,它利用一个名为 RootViewController
的类在视图之间切换.
I'm writing a multiview app that utilizes a class called RootViewController
to switch between views.
在我的 MyAppDelegate
标头中,我创建了一个名为 rootViewController
的 RootViewController
实例.我见过这样的例子,其中 @class 指令用作前向类声明",但我不太确定这意味着什么或完成了什么.
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
.
.
.
推荐答案
它基本上告诉编译器 RootViewController
类存在,而没有具体说明它的样子(即:它的方法、属性、等等).您可以使用它来编写包含 RootViewController
成员变量的代码,而无需包含完整的类声明.
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 includes RootViewController
member variables without having to include the full class declaration.
这在解决循环依赖方面特别有用 - 例如,假设 ClassA
有一个 ClassB*
类型的成员,而 ClassB
有ClassA*
类型的成员.在ClassA
中使用之前需要声明ClassB
,但是在中使用之前还需要声明
.前向声明允许您通过对 ClassA
B类ClassA
说 ClassB
存在来克服这个问题,而不必实际指定 ClassB 的
完整规范.
This is particularly useful in resolving circular dependencies - for example, where say ClassA
has a member of type ClassB*
, and ClassB
has a member of type ClassA*
. You need to have ClassB
declared before you can use it in ClassA
, but you also need ClassA
declared before you can use it in ClassB
. Forward declarations allow you to overcome this by saying to ClassA
that ClassB
exists, without having to actually specify ClassB's
complete specification.
您倾向于发现大量前向声明的另一个原因是有些人采用前向声明类的约定,除非他们绝对必须包含完整声明.我不完全记得,但可能这是 Apple 在其 Objective-C 指导风格指南中推荐的内容.
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.
继续我上面的例子,如果你的 ClassA
和 ClassB
声明在文件 ClassA.h
和 ClassB.h 中
分别,你需要 #import
任何一个在另一个类中使用它的声明.使用前向声明意味着你不需要 #import
,这使得代码更漂亮(特别是一旦你开始收集相当多的类,每个类都需要一个 `#import 在它被使用的地方),并通过最大限度地减少编译器在编译任何给定文件时需要考虑的代码量来提高编译性能.
Continuing my above example, if your declarations of ClassA
and ClassB
are in the files ClassA.h
and ClassB.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.
顺便说一句,尽管问题仅与 Objective-C 中的前向声明有关,但所有后续注释也同样适用于 C 和 C++(可能还有许多其他语言)中的编码,它们也支持前向声明并且通常使用出于同样的目的.
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.
这篇关于Objective-C:前向类声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!