Skip to content
Newer
Older
100644 44 lines (36 sloc) 1.09 KB
a0dbc1b @atomicbird Add NSObject+deallocBlock and NSData+reallyMapped
authored Feb 15, 2012
1 //
2 // NSObject+deallocBlock.m
3 // MapTest
4 //
5 // Created by Tom Harrington on 2/5/12.
6 // Copyright (c) 2012 Atomic Bird, LLC. All rights reserved.
7 //
8
9 #import "NSObject+deallocBlock.h"
10 #import <objc/runtime.h>
11
12 static char *deallocArrayKey = "deallocArrayKey";
13
14 // This class exists for the sole purpose of holding a block that it executes before getting deallocated.
15 @interface DeallocHandler : NSObject
16 @property (readwrite, copy) void (^theBlock)(void);
17 @end
18
19 @implementation DeallocHandler
20 @synthesize theBlock;
21
22 - (void)dealloc
23 {
24 if (theBlock != nil) {
25 theBlock();
26 }
27 }
28 @end
29
30 @implementation NSObject (deallocBlock)
31
32 - (void)addDeallocBlock:(void (^)(void))theBlock;
33 {
34 NSMutableArray *deallocBlocks = objc_getAssociatedObject(self, &deallocArrayKey);
35 if (deallocBlocks == nil) {
36 deallocBlocks = [NSMutableArray array];
37 objc_setAssociatedObject(self, &deallocArrayKey, deallocBlocks, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
38 }
39 DeallocHandler *handler = [[DeallocHandler alloc] init];
40 [handler setTheBlock:theBlock];
41 [deallocBlocks addObject:handler];
42 }
43 @end
Something went wrong with that request. Please try again.