在 Objective-C 接口中 #import 超出绝对必要的部分总是不合适吗?
我很清楚,一般的经验法则是,您应该只导入类编译和使用 @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
首先,我只是前向声明 ClassA
和 ClassB< /code> (因为
classCObjects
的组件仅根据合同属于 ClassC
)。这是我最初的直觉。
但在尝试在其他地方使用 ClassD
后,我很快意识到我还必须导入 ClassA
、ClassB
和 ClassC
> 以及我在任何地方使用它的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
虽然您通常遵循的策略(不要导入不必要的内容)是很棒的、优雅的风格并且总体上是一个很好的目标,但不要太担心棘手的情况。只需
#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-
#import
ing 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. :)作为一名程序员,应该尽可能地提前声明类。它大大减少了大型项目的编译时间。但是花费太多时间在哪里转发声明并不能帮助您设计一个非常好的有吸引力的应用程序。因此,如果您可以转发声明,就可以实现,但不是必须的。
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.