forward declaration for objective-c interfaces

為{幸葍}努か 提交于 2019-12-09 14:34:51

问题


How do I forward declare this object:

@interface MyClass : NSObject <AVAudioSessionDelegate>
{

}

@end

in objective c


回答1:


This is a forward declaration of an ObjC type:

@class MyClass;

And this is a forward declaration of an ObjC protocol:

@protocol AVAudioSessionDelegate;

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.



来源:https://stackoverflow.com/questions/16789252/forward-declaration-for-objective-c-interfaces

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!