From d4a794459b9f2179006853fd0a1528c5650a9949 Mon Sep 17 00:00:00 2001 From: Dan Ziemba Date: Fri, 4 May 2012 06:53:45 -0400 Subject: [PATCH 1/2] Enable compiling with ARC and non-ARC --- ARCMacros.h | 66 +++++++++++++++++++++++++++++++++++++++++++++ PullToRefreshView.h | 3 ++- PullToRefreshView.m | 16 +++++------ README | 3 ++- 4 files changed, 78 insertions(+), 10 deletions(-) create mode 100644 ARCMacros.h diff --git a/ARCMacros.h b/ARCMacros.h new file mode 100644 index 0000000..b65aa98 --- /dev/null +++ b/ARCMacros.h @@ -0,0 +1,66 @@ +// +// ARCMacros.h +// InnerBand +// +// For an explanation of why these work, see: +// +// http://raptureinvenice.com/arc-support-without-branches/ +// +// Created by John Blanco on 1/28/12. +// Copyright (c) 2012 Rapture In Venice. All rights reserved. +// +// NOTE: __bridge_tranfer is not included here because releasing would be inconsistent. +// Avoid it unless you're using ARC exclusively or managing it with __has_feature(objc_arc). +// + +#if !defined(__clang__) || __clang_major__ < 3 + #ifndef __bridge + #define __bridge + #endif + + #ifndef __bridge_retain + #define __bridge_retain + #endif + + #ifndef __bridge_retained + #define __bridge_retained + #endif + + #ifndef __autoreleasing + #define __autoreleasing + #endif + + #ifndef __strong + #define __strong + #endif + + #ifndef __unsafe_unretained + #define __unsafe_unretained + #endif + + #ifndef __weak + #define __weak + #endif +#endif + +#if __has_feature(objc_arc) + #define SAFE_ARC_PROP_RETAIN strong + #define SAFE_ARC_RETAIN(x) (x) + #define SAFE_ARC_RELEASE(x) + #define SAFE_ARC_AUTORELEASE(x) (x) + #define SAFE_ARC_BLOCK_COPY(x) (x) + #define SAFE_ARC_BLOCK_RELEASE(x) + #define SAFE_ARC_SUPER_DEALLOC() + #define SAFE_ARC_AUTORELEASE_POOL_START() @autoreleasepool { + #define SAFE_ARC_AUTORELEASE_POOL_END() } +#else + #define SAFE_ARC_PROP_RETAIN retain + #define SAFE_ARC_RETAIN(x) ([(x) retain]) + #define SAFE_ARC_RELEASE(x) ([(x) release]) + #define SAFE_ARC_AUTORELEASE(x) ([(x) autorelease]) + #define SAFE_ARC_BLOCK_COPY(x) (Block_copy(x)) + #define SAFE_ARC_BLOCK_RELEASE(x) (Block_release(x)) + #define SAFE_ARC_SUPER_DEALLOC() ([super dealloc]) + #define SAFE_ARC_AUTORELEASE_POOL_START() NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; + #define SAFE_ARC_AUTORELEASE_POOL_END() [pool release]; +#endif diff --git a/PullToRefreshView.h b/PullToRefreshView.h index f2b96ef..cd89406 100644 --- a/PullToRefreshView.h +++ b/PullToRefreshView.h @@ -28,6 +28,7 @@ #import #import +#import "ARCMacros.h" typedef enum { kPullToRefreshViewStateUninitialized = 0, @@ -41,7 +42,7 @@ typedef enum { @protocol PullToRefreshViewDelegate; @interface PullToRefreshView : UIView { - id delegate; + __unsafe_unretained id delegate; UIScrollView *scrollView; PullToRefreshViewState state; diff --git a/PullToRefreshView.m b/PullToRefreshView.m index 607cc9a..ea7efef 100644 --- a/PullToRefreshView.m +++ b/PullToRefreshView.m @@ -65,7 +65,7 @@ - (id)initWithScrollView:(UIScrollView *)scroll { CGRect frame = CGRectMake(0.0f, 0.0f - scroll.bounds.size.height, scroll.bounds.size.width, scroll.bounds.size.height); if ((self = [super initWithFrame:frame])) { - scrollView = [scroll retain]; + scrollView = SAFE_ARC_RETAIN(scroll); [scrollView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:NULL]; self.autoresizingMask = UIViewAutoresizingFlexibleWidth; @@ -127,7 +127,7 @@ - (void)refreshLastUpdatedDate { [formatter setPMSymbol:@"PM"]; [formatter setDateFormat:@"MM/dd/yy hh:mm a"]; subtitleLabel.text = [NSString stringWithFormat:@"Last Updated: %@", [formatter stringFromDate:date]]; - [formatter release]; + SAFE_ARC_RELEASE(formatter); } - (void)beginLoading { @@ -229,7 +229,7 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N - (void)containingViewDidUnload { [scrollView removeObserver:self forKeyPath:@"contentOffset"]; - [scrollView release]; + SAFE_ARC_RELEASE(scrollView); scrollView = nil; } @@ -238,14 +238,14 @@ - (void)dealloc { if (scrollView != nil) { // probably leaking the scrollView NSLog(@"PullToRefreshView: Leaking a scrollView?"); - [scrollView release]; + SAFE_ARC_RELEASE(scrollView); } - [arrowImage release]; - [statusLabel release]; - [subtitleLabel release]; + SAFE_ARC_RELEASE(arrowImage); + SAFE_ARC_RELEASE(statusLabel); + SAFE_ARC_RELEASE(subtitleLabel); - [super dealloc]; + SAFE_ARC_SUPER_DEALLOC(); } @end diff --git a/README b/README index 86c57ff..13ce600 100644 --- a/README +++ b/README @@ -4,9 +4,10 @@ It is: - a pull-to-refresh implementation - very easy to implement - doesn't suck + - compatable with ARC and non-ARC To implement it: - - add the four files (PullToRefreshView.{h,m}, arrow.png and arrow@2x.png) to your project + - add the five files (PullToRefreshView.{h,m}, ARCMacros.h, arrow.png and arrow@2x.png) to your project - add the Quartz framework to your project if you haven't done so yet - #import "PullToRefreshView.h" - add QuartzCore to your project From a99db18a5f5ea14b4c306d1831f85c853d1e6759 Mon Sep 17 00:00:00 2001 From: Dan Ziemba Date: Fri, 15 Jun 2012 09:13:49 -0400 Subject: [PATCH 2/2] Renamed ARCMacros.h --- ARCMacros.h => PullToRefreshARCMacros.h | 0 PullToRefreshView.h | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename ARCMacros.h => PullToRefreshARCMacros.h (100%) diff --git a/ARCMacros.h b/PullToRefreshARCMacros.h similarity index 100% rename from ARCMacros.h rename to PullToRefreshARCMacros.h diff --git a/PullToRefreshView.h b/PullToRefreshView.h index cd89406..113b5a6 100644 --- a/PullToRefreshView.h +++ b/PullToRefreshView.h @@ -28,7 +28,7 @@ #import #import -#import "ARCMacros.h" +#import "PullToRefreshARCMacros.h" typedef enum { kPullToRefreshViewStateUninitialized = 0,