0

When I am using this method class_getName(), i find it is declared like this:

**OBJC_EXPORT** const char *class_getName(Class cls)

What does OBJC_EXPORT mean?

Bista
  • 7,869
  • 3
  • 27
  • 55
moon.pike
  • 1
  • 3

2 Answers2

2

If you CMD+CLICK on the symbol, you'll see it's not a keyword. OBJC_EXPORT is defined as:

#define OBJC_EXPORT  OBJC_EXTERN OBJC_VISIBLE

And OBJC_EXTERN is either extern "C" or extern, depending on if you're compiling C++ or C, respectively.

And OBJC_VISIBLE is either __declspec(dllexport) or __declspec(dllimport) on Windows, or __attribute__((visibility("default")) otherwise.

Basically it's saying you can link that symbol externally.

Here's the full listing:

#if !defined(OBJC_EXTERN)
#   if defined(__cplusplus)
#       define OBJC_EXTERN extern "C" 
#   else
#       define OBJC_EXTERN extern
#   endif
#endif

#if !defined(OBJC_VISIBLE)
#   if TARGET_OS_WIN32
#       if defined(BUILDING_OBJC)
#           define OBJC_VISIBLE __declspec(dllexport)
#       else
#           define OBJC_VISIBLE __declspec(dllimport)
#       endif
#   else
#       define OBJC_VISIBLE  __attribute__((visibility("default")))
#   endif
#endif

#if !defined(OBJC_EXPORT)
#   define OBJC_EXPORT  OBJC_EXTERN OBJC_VISIBLE
#endif
i_am_jorf
  • 53,608
  • 15
  • 131
  • 222
0

If you have three class named A,B and C respectively and all three classes are unrelated(i mean not in inheritance hierarchy) and you just want to access the value declared in A, then extern is a more appropriate way to go. In this case, you want to declare the variable as extern in ClassA.h, then define it in Class.m. As long as ClassB and ClassC import ClassA.h, they will be able to link against the same extern definition.

Instead of using extern by itself, it's more robust to use OBJC_EXPORT, which is defined in objc-api.h and handles compiling under C++ as well. Here's a code sample:

// ClassA.h
OBJC_EXPORT NSString* commonString;
...

// ClassA.m
NSString* commonString = @"OldValue";

// ClassB.m
#import "ClassA.h"
...
commonString = @"NewValue"; // Can be inside a function or method

Reference : this so post

Community
  • 1
  • 1
Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75