Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to require ARC in a class?

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.

like image 869
adonoho Avatar asked Dec 04 '11 22:12

adonoho


People also ask

What is ARC and non arc?

The simple answer is that in non-ARC projects you have to control almost all memory operations (ownership, release time and etc.) by yourself. On the other hand, in ARC enabled projects most work done by system.

Does Objective C support Arc?

Automatic Reference Counting (ARC) is a memory management option for Objective-C provided by the Clang compiler. When compiling Objective-C code with ARC enabled, the compiler will effectively retain, release, or autorelease where appropriate to ensure the object's lifetime extends through, at least, its last use.


1 Answers

The following should work:

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

Place it at the top of your file.

like image 120
Krizz Avatar answered Sep 24 '22 09:09

Krizz