forward declaration for objective-c interfaces

前端 未结 1 571
难免孤独
难免孤独 2021-02-19 14:17

How do I forward declare this object:

@interface MyClass : NSObject 
{

}

@end

in objective c

1条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-19 15:01

    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.

    0 讨论(0)
提交回复
热议问题