10

in same file we want to write code which supports ARC and non-ARC. For that some macro required.

#ifdef ARC_ENABLED 
NSLog(@" ARC enabled ");
#else
NSLog(@" ARC disabled ");
[self release];
#endif

How to achieve this macro, Does any kind of macro available? Please let me know. Advance thanks for support Note: ARC_ENABLED Just i have written for example

Krishnabhadra
  • 34,169
  • 30
  • 118
  • 167
Ajay
  • 349
  • 3
  • 12
  • possible duplicate of [Conditional compilation when using ARC](http://stackoverflow.com/questions/8447090/conditional-compilation-when-using-arc) – kennytm Jun 07 '12 at 10:43
  • possible duplicate of [How to programmatically detect automatic reference counting?](http://stackoverflow.com/questions/10700661/how-to-programmatically-detect-automatic-reference-counting) – jscs Jun 07 '12 at 18:27

2 Answers2

27

There is an objective C macro __has_feature, you can use that to check whether arc is enabled for not.

From Clang Language Extension documentation

Automatic reference counting

Clang provides support for automated reference counting in Objective-C, which eliminates the need for manual retain/release/autorelease message sends. There are two feature macros associated with automatic reference counting: __has_feature(objc_arc) indicates the availability of automated reference counting in general, while __has_feature(objc_arc_weak) indicates that automated reference counting also includes support for __weak pointers to Objective-C objects.

The section Feature checking macro's is a very good read.

You can use it like this..

#if !__has_feature(objc_arc)
    //Do manual memory management...
#else
    //Usually do nothing...
#endif

The code part shamelessly copied from this answer.

Community
  • 1
  • 1
Krishnabhadra
  • 34,169
  • 30
  • 118
  • 167
  • @Krishnabhadra, how to define __has_feature(objc_arc), I did something like below #ifndef __has_feature #define __has_feature(x) 0 #endif #ifndef __has_extension #define __has_extension __has_feature // Compatibility with pre-3.0 compilers. #endif #if __has_feature(objc_arc) && __clang_major__ >= 3 #define ARC_ENABLED 1 #else #define ARC_ENABLED 0 #endif // __has_feature(objc_arc) But it seems it does not work, i have used retain and release when i enabled ARC in project, gives error – Ajay Jun 07 '12 at 10:51
  • +1 Great answer. `__has_feature` is a wonderful macro, and can be used for more than just ARC - you can check for a lot of Objective-C features (ex. modules). – Sam Spencer Jan 11 '14 at 18:15
2

The following will define USING_ARC, USING_MRC & USING_GC to be 0 or 1, along with a few sanity checks:

// Utility macros (undefined below)

#define PREFIX_ONE(a) 1##a
#define EMPTY_DEFINE(a) (PREFIX_ONE(a) == 1)

// Memory management kind

#if !defined(USING_GC)
#  if defined(__OBJC_GC__)
#     define USING_GC 1
#  else
#    define USING_GC 0
#  endif
#elif EMPTY_DEFINE(USING_GC) 
#   undef USING_GC
#   define USING_GC 1
#endif

#if !defined(USING_ARC)
#  if __has_feature(objc_arc)
#     define USING_ARC 1
#  else
#    define USING_ARC 0
#  endif
#elif EMPTY_DEFINE(USING_ARC)
#   undef USING_ARC
#   define USING_ARC 1
#endif

#if !defined(USING_MRC)
#  if USING_ARC || USING_GC
#     define USING_MRC 0
#  else
#    define USING_MRC 1
#  endif
#elif EMPTY_DEFINE(USING_MRC)
#   undef USING_MRC
#   define USING_MRC 1
#endif

// Remove utility

#undef PREFIX_ONE
#undef EMPTY_DEFINE

// Sanity checks

#if USING_GC
#   if USING_ARC || USING_MRC
#      error "Cannot specify GC and RC memory management"
#   endif
#elif USING_ARC
#   if USING_MRC
#      error "Cannot specify ARC and MRC memory management"
#   endif
#elif !USING_MRC
#   error "Must specify GC, ARC or MRC memory management"
#endif

#if USING_ARC
#   if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_6
#      error "ARC requires at least 10.6"
#   endif
#endif

Place that in a suitable .h included in your project .pch

You can now #if USING_x to control conditional compilation anywhere.

Also you can rule out some files from compiling under certain memory models by including, for example, at the top of the file:

#if USING_GC | USING_ARC
   #error "Sorry, this file only works with MRC"
#endif
CRD
  • 52,522
  • 5
  • 70
  • 86