objective-c接口的前向声明 [英] forward declaration for objective-c interfaces

查看:148
本文介绍了objective-c接口的前向声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何转发声明此对象:

@interface MyClass : NSObject <AVAudioSessionDelegate>
{

}

@end

in objective c

in objective c

推荐答案

这是ObjC类型的前向声明:

This is a forward declaration of an ObjC type:

@class MyClass;

这是ObjC协议的前向声明:

And this is a forward declaration of an ObjC protocol:

@protocol AVAudioSessionDelegate;






对于那些好奇的人,为什么这是有用:前向声明可用于显着减少依赖性并显着减少构建时间,因为它允许您避免 #import ing标头和/或整个框架(然后 #import 其他框架)。当不使用前向声明时,程序的其他部分可以看到许多不必要的标题 - 更改一个标题会导致重新编译许多文件,并且编译和链接时间会增加。因为ObjC类型总是作为指针处理(在我们的抽象级别),所以在大多数情况下前向声明就足够了。然后,您可以在 @implementation 或类继续中声明您的ivars,然后 #import 可以进入 *。m 文件。另一个原因是避免循环依赖。


For those of you who are curious why this is useful: Forward declarations can be used to significantly reduce your dependencies and significantly reduce your build times because it allows you to avoid #importing headers and/or entire frameworks (which then #import other frameworks). When forward declarations are not used, many unnecessary headers are visible to other parts of your program -- changing one header can cause many files to be recompiled, and the compilation and link times will go up. Because ObjC types are always dealt with as pointers (at our abstraction level), the forward declaration is sufficient in most cases. You can then declare your ivars in your @implementation or class continuation and the #import can then go in the *.m file. Another reason is to avoid circular dependencies.

这篇关于objective-c接口的前向声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆