9

I have an app with both ARC code and non-ARC code. The compiler will catch when I try to compile non-ARC code as ARC. How do I cause a compile time error/notice when my ARC code is erroneously compiled without ARC? Obviously, the code will compile. It will just leak. The static analyzer will catch the problem. I would rather find a way to leave a pragma or define in my ARC code.

The following is defined by Apple in objc-api.h:

/* OBJC_ARC_UNAVAILABLE: unavailable with -fobjc-arc */
#if !defined(OBJC_ARC_UNAVAILABLE)
#   if __has_feature(objc_arr)
#       define OBJC_ARC_UNAVAILABLE __attribute__((unavailable("not available in automatic reference counting mode")))
#   else
#       define OBJC_ARC_UNAVAILABLE
#   endif
#endif

My C-macro-fu is weak. How would I use it? Or, perhaps there is a better symbol to check?

P.S. I ask because I build much of my app from reusable libraries. I want to ensure that each file is compiled in the right way.

jscs
  • 63,694
  • 13
  • 151
  • 195
adonoho
  • 4,339
  • 1
  • 18
  • 22

2 Answers2

11

The following should work:

#if !__has_feature(objc_arc)
#  error Compile me with ARC, please!
#endif

Place it at the top of your file.

Krizz
  • 11,362
  • 1
  • 30
  • 43
-1

If it is a complete file, I would recommend adding -fno-objc-arc to the compiler flags to use the non-arc compiler.

Use the macro to compile "parts" of code with the non-arc compiler. for example, use this if you are writing a framework that will be used in both arc and non-arc code bases and write release, deallocs within this macro block

Mugunth
  • 14,461
  • 15
  • 66
  • 94
  • Mugunth, as modern Xcode, v4.2+, allows mixed ARC and non-ARC code in a project and ARC supports apps back to iOS v4.0, I prefer to avoid the macro route and just move the file over to ARC as I add new functionality. The key is to ensure this change is propagated as new versions of my and my client's apps are revved. The above macro does this. Thank you for your comment, thought and time. Andrew – adonoho Feb 17 '12 at 17:04