Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

__OBJC__ in objective C

Tags:

What does __OBJC__ mean in Objective C?

#import <Availability.h>  #ifdef __OBJC__     #import <Foundation/Foundation.h>     #import <UIKit/UIKit.h> #endif 
like image 492
Casebash Avatar asked Jan 13 '10 05:01

Casebash


People also ask

What is OBJC?

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.

What does @() do OBJC?

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.

Can C++ call Objective-C?

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.

Is Objective-C compatible with C?

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.


2 Answers

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 
like image 119
Chris Becke Avatar answered Oct 21 '22 14:10

Chris Becke


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.

like image 29
Seva Alekseyev Avatar answered Oct 21 '22 16:10

Seva Alekseyev