What does __OBJC__
mean in Objective C?
#import <Availability.h> #ifdef __OBJC__ #import <Foundation/Foundation.h> #import <UIKit/UIKit.h> #endif
Objective-C is the primary programming language you use when writing software for OS X and iOS. It's a superset of the C programming language and provides object-oriented capabilities and a dynamic runtime.
In Objective-C, any character , numeric or boolean literal prefixed with the '@' character will evaluate to a pointer to an NSNumber object (In this case), initialized with that value. C's type suffixes may be used to control the size of numeric literals.
You can transform your Objective-C file (. m) to Objective-C++ (. mm). This by default enables you to call any C++ code in your Objective-C++ file.
Objective C is a set of backward-compatible extensions to C. This is possible because the Objective C features are delimited in two very simple ways: use of the character @ . This character is not currently used in the C language.
It means the objective C compiler is being used. So you can create hybrid header files that can be used when compiling objective C or C or C++.
You could use it in a header file like this, if you wanted to publish a header file that defined an objective c object that you wanted to make available to c and c++ programmers/code :
#ifndef MYHEADER_H #define MYHEADER_H #ifdef __OBJC__ // Put objective C things in this block // This is an objc object implemented in a .m or .mm file @implementation some_objc_object { } @end #endif #ifdef __cplusplus #define CLINKAGE "C" // c++ things that .m or .c files wont understand go in here // This class, in a .mm file, would be able to call the obj-c objects methods // but present a c++ interface that could be called from c++ code in .cc or .cpp // files class SomeClassThatWrapsAnObjCObject { id idTheObject; public: // ... }; #endif // and here you can declare c functions and structs // this function could be used from a .c file to call to a .m file and do something // with the object identified by id obj extern CLINKAGE somefunction(id obj, ...); #endif // MYHEADER_H
This looks like your precompiled header file.
The precompiled header is shared between all C-dialect files in your project. It's as if all your .c, .cpp, .m and .mm files have an invisible #include directive as the first line. But the Cocoa header files are pure Objective C - trying to include them in a C/C++ source will yield nothing but syntax errors aplenty. Thus the #ifdef.
If your project only contains Objective C files (.m/.mm), which is the typical case, the #ifdef is not really necessary. But Xcode, which generated this header in the first place, protects you all the same.
Even if it's not a PCH file, this #ifdef only makes sense if the file is to be included from both Objective C and plain C/C++. But it does not hurt regardless.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With