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*
的成员.您必须先声明ClassB
才能在ClassA
中使用它,但还需要声明ClassA
才可以在ClassB
中使用它.前向声明使您可以通过对ClassA
说ClassB
存在而克服此问题,而不必实际指定ClassB's
完整规范.
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.
您倾向于找到很多前向声明的另一个原因是,有些人采用了前向声明类的约定,除非他们绝对必须包含完整的声明.我还没有完全记得,但是苹果可能会在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屋!