Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

#include or #import <objc/runtime.h>?

For an iPhone app, should I #include or #import , and why?

I've seen it done both ways, e.g., #import & #include.

like image 508
ma11hew28 Avatar asked Feb 27 '12 23:02

ma11hew28


2 Answers

If the header file has traditional include guards, it really doesn't matter which you use, it's more of a stylistic choice. There might be a tiny performance boost if you use #import instead of #include, but I doubt it will be noticeable, since most compilers these days are smart enough to recognize include guards and optimize accordingly.

If, on the other hand, the header file does not have include guards, then you should always use #import, since #import will ensure that the header will only get included once -- if you accidentally #include such a header twice, you will almost certainly get a deluge of compiler errors about redefinitions etc.

Since most Objective-C headers (especially those coming from the Objective-C runtime or the Cocoa headers) do not have include guards, you should use #import when including those. When including standard C library headers or headers from a third-party library, it doesn't matter which you choose -- pick one style and be consistent.

like image 97
Adam Rosenfield Avatar answered Sep 23 '22 02:09

Adam Rosenfield


use #import. the advantage is that it does not "re-include" files if they've already been included.

like image 41
pickwick Avatar answered Sep 24 '22 02:09

pickwick