jhflink
jhflink

Reputation: 41

Can I #ifdef #imports?

Can I #ifdef #imports in objective-c?

For example:

#ifdef USE_A
#import "ClassA.h"
#endif

#ifdef USE_B
#import "ClassB.h"
#endif

Upvotes: 3

Views: 3500

Answers (3)

Parag Bafna
Parag Bafna

Reputation: 22930

Yes, you can use #ifdef #imports in objective-c.

 #ifdef MACRO

 controlled text

 #endif /* MACRO */

This block is called a conditional group. controlled text will be included in the output of the preprocessor if and only if MACRO is defined. We say that the conditional succeeds if MACRO is defined, fails if it is not. For more information take a look at GCC online docs.

Upvotes: 0

justin
justin

Reputation: 104698

Yes, this:

#ifdef USE_A
#import "ClassA.h"
#endif

is valid.

Upvotes: 6

Srikar Appalaraju
Srikar Appalaraju

Reputation: 73648

I believe its #ifdef __OBJC__ directive to ensure that following libraries for Objective-C are imported. The purpose of that if, is to not import them unless it is necessary. Also, this way the 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.

#ifdef __OBJC__
#import <foundation/foundation.h>
#import <uikit/uikit.h>
#import <coredata/coredata.h>
#endif

Upvotes: 1

Related Questions