Objective-C:前向类声明

发布于 2024-10-20 06:38:22 字数 345 浏览 4 评论 0原文

我正在编写一个多视图应用程序,它利用一个名为 RootViewController 的类在视图之间切换。

在我的 MyAppDelegate 标头中,我创建了一个名为 rootViewControllerRootViewController 实例。我见过这样的例子,其中 @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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

氛圍 2024-10-27 06:38:22

它基本上告诉编译器类 RootViewController 存在,但没有指定它到底是什么样子(即:它的方法、属性等)。您可以使用它来编写包含 RootViewController 成员变量的代码,而无需包含完整的类声明。

这在解决循环依赖关系时特别有用 - 例如,其中 ClassA 有一个 ClassB* 类型的成员,而 ClassB 有一个类型的成员类型ClassA*。您需要先声明 ClassB,然后才能在 ClassA 中使用它,但也需要先声明 ClassA,然后才能在 中使用它B 类。前向声明允许您通过告诉 ClassA 存在 ClassB 来克服这个问题,而不必实际指定 ClassB 的完整规范。

您往往会发现大量前向声明的另一个原因是有些人采用前向声明类的约定,除非他们绝对必须包含完整的声明。我不完全记得了,但这可能是 Apple 在其 Objective-C 指导风格指南中推荐的内容。

继续上面的示例,如果您的 ClassAClassB 声明位于文件 ClassA.hClassB.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 includes RootViewController 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 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.

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 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.

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.

一抹微笑 2024-10-27 06:38:22

前向声明主要是为了避免循环导入,其中一个文件导入另一个文件,该文件导入第一个文件等。基本上,当您导入文件时,文件的内容会在构建项目时在导入点被替换,然后将其馈送给编译器。如果你有循环导入,你就会有一个永远无法编译的无限循环。幸运的是 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.

眼眸里的快感 2024-10-27 06:38:22

@class前向类声明(不完整类型) - 只需告诉编译器该类存在。在这种情况下,编译器知道有关类型内存布局的任何信息 - 类大小、成员或方法。这就是为什么您只能用于通过引用和指针定义类。

优点:

  • 减少构建时间
  • 打破循环引用

@class or forward 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:

  • reduce build time
  • break cyclic references
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文