开发者

What is #ifdef __OBJC__ doing and why libraries listed below?

I believe the #ifdef __OBJC__ directive is ensuring that I import the following class libraries for Objective-C only. What is the purpose of listing the class libraries after the ifdef statement? Doesn't this code example kinda defeat the purpose?

#ifdef __OBJC__
#import <foundation/foundation.h>
#import <uikit/uikit.h>
#import <co开发者_运维问答redata/coredata.h>
#endif
</coredata/coredata.h></uikit/uikit.h></foundation/foundation.h>


Objective-C is a superset of C (just like C++ is) and fairly often files from the different languages will be used in the same project and share headers, especially the prefix header. The #ifdef __OBJC__, like #ifdef __cplusplus, lets you include (or #import for Objective-C) headers for only the appropriate language.

The same header included in .c, .cpp, and .m files (with default compiler settings) would only have __OBJ__ defined for the .m files.


Basically in that code if you are using Objective C it will import those 3 libraries

#import <foundation/foundation.h>
#import <uikit/uikit.h>
#import <coredata/coredata.h>

The purpose of that if, is to not import them unless it is necessary.


They are listed after the #endif just as a reminder, so it makes the code easier to read. Otherwise you'd have to look up above to see what the #endif was ending.


The reason this is done is so that this code can still be compatible with regular C code that may want to use the functionality in that C file (at least that's what it looks like to me). By including those libraries only when OBJC is defined it ensures that the libraries are ONLY imported when you are compiling for objective c and not for standard C.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜