在 Objective-C 接口中 #import 超出绝对必要的部分总是不合适吗?

发布于 2024-10-16 11:49:03 字数 1062 浏览 2 评论 0原文

我很清楚,一般的经验法则是,您应该只导入类编译和使用 @class 所必需的内容(基类接口、协议接口等)。进行前瞻性声明。然而,我遇到了以下场景,我觉得 #import 是一个更合适的解决方案:

#import "ClassA.h"      // Previously, @class ClassA;
#import "ClassB.h"      // Previously, @class ClassB;
#import "ClassC.h"      // Unnecessary to forward-declare

@interface ClassD : NSObject

@property (nonatomic, retain) ClassA *  classAObject;
@property (nonatomic, retain) ClassB *  classBObject;
@property (nonatomic, copy)   NSArray * classCObjects;

@end

首先,我只是前向声明 ClassAClassB< /code> (因为 classCObjects 的组件仅根据合同属于 ClassC)。这是我最初的直觉。

但在尝试在其他地方使用 ClassD 后,我很快意识到我还必须导入 ClassAClassBClassC > 以及我在任何地方使用它的 ClassD 。这似乎是其他类在使用 ClassD 时不必关心的事情。我的想法是,基本上,ClassD 的用户应该只关心导入 ClassD.h 并假设它可以与整个类一起使用,而无需一堆其他>#import 语句。鉴于上述方法,我基本上已经在其接口中包含了在 ClassD 域内工作所需的所有内容。

除了“您包含的内容超出了编译绝对必要的范围”之外,是否有充分的理由说明这种方法不理想?

I'm well aware that the general rule of thumb is you should only import what is necessary—base class interfaces, protocol interfaces, etc.—for a class to compile and use @class for everything that can be forward-declared. However, I ran into the following scenario in which I felt like #import was a more appropriate solution:

#import "ClassA.h"      // Previously, @class ClassA;
#import "ClassB.h"      // Previously, @class ClassB;
#import "ClassC.h"      // Unnecessary to forward-declare

@interface ClassD : NSObject

@property (nonatomic, retain) ClassA *  classAObject;
@property (nonatomic, retain) ClassB *  classBObject;
@property (nonatomic, copy)   NSArray * classCObjects;

@end

At first, I simply forward-declared ClassA and ClassB (as the components of classCObjects are of ClassC only by contract). This was my initial instinct.

But after trying to use ClassD elsewhere, I quickly realized that I had to also import ClassA, ClassB, and ClassC along with ClassD everywhere I used it. This seems like something another class shouldn't have to care about when using ClassD. My thinking was that, basically, a user of ClassD should really only care about importing ClassD.h and assume it can work with the entire class without a bunch of other #import statements. Given the above approach, I've basically included everything necessary to work within the domain of ClassD right in its interface.

Is there a strong reason why this approach is not ideal, except for "you've included more than is absolutely necessary for compilation?"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

何其悲哀 2024-10-23 11:49:03

虽然您通常遵循的策略(不要导入不必要的内容)是很棒的、优雅的风格并且总体上是一个很好的目标,但不要太担心棘手的情况。只需 #import 您需要的内容。

实际上,在现代机器上,过度#import的唯一影响是编译时间增加了几微秒。利用您节省的开发时间来让您的应用程序变得更加出色。 :)

While the strategy you're generally following-- don't import more than necessary--is awesome, classy style and a great thing to aim for in general, don't sweat the tricky cases too much. Just #import what you need.

In practice, on a modern machine, the only effect over-#importing will have is to add a few unobservable microseconds to your compile time. Use the developer time you'll save for making your app awesomer. :)

暮倦 2024-10-23 11:49:03

作为一名程序员,应该尽可能地提前声明类。它大大减少了大型项目的编译时间。但是花费太多时间在哪里转发声明并不能帮助您设计一个非常好的有吸引力的应用程序。因此,如果您可以转发声明,就可以实现,但不是必须的

As a programmer, one should go forward declaration of classes where ever it is possible. It considerably reduces compilation time on big projects. But spending too much time on where to forward declare isn't any way going to help you design a very good attractive app. So, if you can forward declare do it but it is not a must.

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