0

I am learning the differences between #include and #import in objective c. I searched from the Internet and learned that, the #include could cause recursive problem. If you use #import, this problem can be avoid. But I do not understand this, first what is recursive includes and then how #import can prevent this problem happening?

Jay
  • 113
  • 1
  • 11

1 Answers1

1

Recursion is the process of repeating items in a self-similar way.

The same way you can call a function inside of other functions, you can call a function inside of itself. A function that calls itself is called a recursive function. Recursion is important because you can solve some problems by solving similar sub-problems. Recursive solutions usually have less code and are more elegant that their iterative equivalents if the problem you solve is recursive in nature.

What are the differences between #import and #include ?

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 Objective-C things (like class definitions and such) and #include standard C stuff that I need. For example, one of my source files might look like this:

#import <Foundation/Foundation.h>

#include <asl.h>
#include <mach/mach.h>

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

Community
  • 1
  • 1
emresancaktar
  • 1,507
  • 17
  • 25