__OBJC__ in objective C

后端 未结 3 1717
悲&欢浪女
悲&欢浪女 2021-02-01 04:53

What does __OBJC__ mean in Objective C?

#import 

#ifdef __OBJC__
    #import 
    #import <         


        
相关标签:
3条回答
  • 2021-02-01 05:06

    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
    
    0 讨论(0)
  • 2021-02-01 05:22

    Its just a macro symbol. In this case if that symbol is defined then your program should import the Apple Cocoa frameworks (Foundation and AppKit).

    This woudl be the case if you were developing an objective-c / cocoa application. In other words, if you were developing a C++ / carbon application, the __OBJC__ symbol would not be defined and those objective-c dependant frameworks would not be imported.

    0 讨论(0)
  • 2021-02-01 05:24

    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.

    0 讨论(0)
提交回复
热议问题