__OBJC__ in objective C

后端 未结 3 1718
悲&欢浪女
悲&欢浪女 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
    

提交回复
热议问题