-
-
Save nicklockwood/1563325 to your computer and use it in GitHub Desktop.
// | |
// 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 |
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.
@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.
@nverinaud you may prefer this older version if you are worried about performance:
https://gist.github.com/nicklockwood/1563325/4d32686a6058cb9bc409767ee7cc37a7993cb2df
Xcode gave me "Extra tokens at end of #undef directive" warning message.
#undef ah_autorelease autorelease
#undef ah_dealloc dealloc
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