2

For the mixed code, I am wondering when to use include and when to use import.

I checked some documents, import is better than include since it can eliminate the need to use include guard (pragma once).

I am wondering if we can totally use import in our mixed code?

jszumski
  • 7,430
  • 11
  • 40
  • 53
Adam Lee
  • 24,710
  • 51
  • 156
  • 236

2 Answers2

1

This issue has long answer is in Stackoverflow:

import

In the C language, the #include pre-compile directive always causes a file's contents to be inserted into the source at that point. Objective-C has the equivalent #import directive except each file is included only once per compilation unit, obviating the need for include guards.

#import and #include

"#import" Objective-C/Objective-C++ headers, and #include C/C++ headers. Choose between #import and #include based on the language of the header that you are including.

When including a header that uses Objective-C or Objective-C++, use #import. When including a standard C or C++ header, use #include. The header should provide its own #define guard. Some Objective-C headers lack #define guards, and expect to be included only by #import. As Objective-C headers may only be included in Objective-C source files and other Objective-C headers, using #import across the board is appropriate.

Standard C and C++ headers without any Objective-C in them can expect to be included by ordinary C and C++ files. Since there is no #import in standard C or C++, such files will be included by #include in those cases. Using #include for them in Objective-C source files as well means that these headers will always be included with the same semantics.

This rule helps avoid inadvertent errors in cross-platform projects. A Mac developer introducing a new C or C++ header might forget to add #define guards, which would not cause problems on the Mac if the new header were included with #import, but would break builds on other platforms where #include is used. Being consistent by using #include on all platforms means that compilation is more likely to succeed everywhere or fail everywhere, and avoids the frustration of files working only on some platforms.

#import <Cocoa/Cocoa.h>
#include <CoreFoundation/CoreFoundation.h>
#import "GTMFoo.h"
#include "base/basictypes.h"

The #import directive was added to Objective-C as an improved version of #include. Whether or not it's improved, however, is still a matter of debate. #import ensures that a file is only ever included once so that you never have a problem with recursive includes. However, most decent header files protect themselves against this anyway, so it's not really that much of a benefit.

Basically it's up to you to decide which you want to use. I tend to #import headers for Object-C things (like class definitions and such) and #include standard C stuff that I need.

Look this links, have more answers:

What is the difference between #import and #include in Objective-C?

Difference Between includes and imports [duplicate]

External documentation links: Objective-C Style Guide

The difference between #import, #include and @class in Objective C

Community
  • 1
  • 1
SBotirov
  • 13,872
  • 7
  • 59
  • 81
0

If you mean standard C code, then you should always be able to use #import.

Also, if you are in a .h file and need to just be able to use the name of a class in, say, an @property, you can use @class <myClassName> without a .h or .m to just use a class name.

Undo
  • 25,519
  • 37
  • 106
  • 129