Skip to content

Instantly share code, notes, and snippets.

@danielctull
Created November 9, 2011 12:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save danielctull/1351298 to your computer and use it in GitHub Desktop.
Save danielctull/1351298 to your computer and use it in GitHub Desktop.
ARC macros for weak references
#import <Availability.h>
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_4_3
#warning "This library uses ARC which is only available in iOS SDK 4.3 and later."
#endif
#if !defined dct_weak && __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_5_0
#define dct_weak weak
#define __dct_weak __weak
#define dct_nil(x)
#elif !defined dct_weak && __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_3
#define dct_weak unsafe_unretained
#define __dct_weak __unsafe_unretained
#define dct_nil(x) x = nil
#endif
@danielctull
Copy link
Author

These are the macros I use to define weak variables for ARC code where the minimum required OS is unknown, i.e. my common classes. This way, it doesn't matter if the minimum target OS is iOS 4.3 or iOS 5, the code will run as optimally as possible.

@Abizern
Copy link

Abizern commented Nov 10, 2011

The trouble with these macros is that once dct_weak is defined control drops through to the final #else and the compiler starts emitting the warning. I've corrected it with https://gist.github.com/1354106 which explicitly emits a warning if the minimum iOS requirement is not met.

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