Snippet: Macros for ARC-agnostic Code

In an ideal World, we would all be using ARC now and all our code bases would have been converted to ARC but, if like me, you still have some legacy code you may find yourself needed to make some code work both with and without ARC.

I wrote the following macros to make converting old non-ARC code to ARC-agnostic code. It’s by no means an exhaustive set, but it may save you some time if you find yourself needing to do the same thing.

Update: In the first posting of this code the ARC versions of retain and autorelease were incorrect. User sophtwhere was kind enough to send solutions to the problem.


About idz

A professional software engineer dabbling in iOS app development.
This entry was posted in Code Snippets, Objective-C. Bookmark the permalink.

4 Responses to Snippet: Macros for ARC-agnostic Code

  1. sophtwhere says:

    not sure how you code objective c, but those macros won’t work in some situations.

    consider the case of

    return [anObject autorelease];

    this would mean that “return IDZ_AUTORELEASE(anObject);” evaluates to

    Non ARC (correct)

    return [anObject autorelease];

    ARC (incorrect)

    return ;

    or the case of

    NSString *myString = [passedInParam retain];

    this would mean that “NSString *myString =IDZ_RETAIN(passedInParam);” evaluates to

    Non ARC (correct)

    NSString *myString = [passedInParam retain];

    ARC (incorrect)

    NSString *myString = ;

    it think you’d need to change it to:

    #if ! __has_feature(objc_arc)
    #define IDZ_RETAIN(_o) [(_o) retain]
    #define IDZ_RELEASE(_o) [(_o) release]
    #define IDZ_AUTORELEASE(_o) [(_o) autorelease]
    #define IDZ_SUPER_DEALLOC [super dealloc]
    #define IDZ_BRIDGE_CAST(_type, _identifier) ((_type)(_identifier))
    #else
    #define IDZ_RETAIN(_o) _o
    #define IDZ_RELEASE(_o)
    #define IDZ_AUTORELEASE(_o) _o
    #define IDZ_SUPER_DEALLOC
    #define IDZ_BRIDGE_CAST(_type, _identifier) ((__bridge _type)(_identifier))
    #endif

  2. sophtwhere says:

    i spent a few hours playing around with your concept and came up with slightly different version of my own.

    basically you add include 2 files in your project:

    NSObject+arcAgnostics.h

    NSObject+arcAgnostics.m

    and ensure that the NSObject+arcAgnostics.m file is compiled with -fno-objc-arc

    any files you want to make arc agnostic (for example “myfile.m” , import the NSObject+arcAgnostics.h file into myfile.m (definitely do not import NSObject+arcAgnostics.h into any other .h file!)

    in an ideal world, all you need to do is change any [super dealloc]; calls to read [SUPER_DEALLOC];

    everything else is handled by some tricky macro cut and pasting.

    enjoy.

Leave a Reply