Skip to content

Instantly share code, notes, and snippets.

@nicklockwood
Last active November 20, 2018 10:02
Show Gist options
  • Save nicklockwood/1563325 to your computer and use it in GitHub Desktop.
Save nicklockwood/1563325 to your computer and use it in GitHub Desktop.
ARC Helper
//
// ARC Helper
//
// Version 2.2
//
// Created by Nick Lockwood on 05/01/2012.
// Copyright 2012 Charcoal Design
//
// Distributed under the permissive zlib license
// Get the latest version from here:
//
// https://gist.github.com/1563325
//
#import <Availability.h>
#undef ah_retain
#undef ah_dealloc
#undef ah_autorelease autorelease
#undef ah_dealloc dealloc
#if __has_feature(objc_arc)
#define ah_retain self
#define ah_release self
#define ah_autorelease self
#define ah_dealloc self
#else
#define ah_retain retain
#define ah_release release
#define ah_autorelease autorelease
#define ah_dealloc dealloc
#undef __bridge
#define __bridge
#undef __bridge_transfer
#define __bridge_transfer
#endif
// Weak reference support
#import <Availability.h>
#if !__has_feature(objc_arc_weak)
#undef ah_weak
#define ah_weak unsafe_unretained
#undef __ah_weak
#define __ah_weak __unsafe_unretained
#endif
// Weak delegate support
#import <Availability.h>
#undef ah_weak_delegate
#undef __ah_weak_delegate
#if __has_feature(objc_arc_weak) && \
(!(defined __MAC_OS_X_VERSION_MIN_REQUIRED) || \
__MAC_OS_X_VERSION_MIN_REQUIRED >= __MAC_10_8)
#define ah_weak_delegate weak
#define __ah_weak_delegate __weak
#else
#define ah_weak_delegate unsafe_unretained
#define __ah_weak_delegate __unsafe_unretained
#endif
// ARC Helper ends
@Coeur
Copy link

Coeur commented Nov 2, 2012

on my fork, I have changed "release" to "ah_release" in order to be compatible with structs CFDictionaryKeyCallBacks and CFDictionaryValueCallBack from CoreFoundation file CFDictionary.h

@nverinaud
Copy link

IMO it is a bad gist, it breaks Convert to Objective-C ARC... tool by hiding release, autorelease and retain calls behind self.

Moreover it reduces performance because you will call self under ARC instead of simply removing calls. Then ARC will add his own release calls instead of using yours.

I suggest wrap your non-ARC code around preprocessor directives like in this file: https://github.com/nverinaud/NVUIGradientButton/blob/master/lib/NVUIGradientButton/NVUIGradientButton.m.

@nicklockwood
Copy link
Author

@nverinaud you're right, but I changed it to use the ah prefix for everything some time ago (before your comment), so that criticism is no longer applicable.

The performance thing is basically a non-issue.

@nicklockwood
Copy link
Author

@nverinaud you may prefer this older version if you are worried about performance:

https://gist.github.com/nicklockwood/1563325/4d32686a6058cb9bc409767ee7cc37a7993cb2df

@hungntv
Copy link

hungntv commented Jul 15, 2013

Xcode gave me "Extra tokens at end of #undef directive" warning message.

#undef ah_autorelease autorelease
#undef ah_dealloc dealloc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment