Created
November 1, 2013 06:21
-
-
Save hiromitsu-murakami/7261555 to your computer and use it in GitHub Desktop.
Blocksの循環参照 Objective-CのBlocksの循環参照に関する僕なりのベストプラクティス
http://blog.katty.in/2612 上記の記事を参考にしたコード
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
#import "FPCommon.h" | |
typedef void (^FPWeakTargetBlock)(typeof_weak_target self); | |
@interface FPAntiCircularReferenceBlockPractice : NSObject | |
- (void)setTarget:(typeof_target)target | |
block:(FPWeakTargetBlock)block; | |
- (void)run; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "FPAntiCircularReferenceBlockPractice.h" | |
@interface FPAntiCircularReferenceBlockPractice () | |
@property (nonatomic, weak) typeof_target target; | |
@property (nonatomic, copy) FPWeakTargetBlock block; | |
@end | |
@implementation FPAntiCircularReferenceBlockPractice | |
- (void)dealloc | |
{ | |
FPCurrent(); | |
} | |
- (void)setTarget:(typeof_target)target | |
block:(FPWeakTargetBlock)block | |
{ | |
self.target = target; | |
self.block = block; | |
} | |
- (void)run | |
{ | |
FPBlocks(self.block)(self.target); | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
// これらの型やマクロを用意することでXcodeの補完機能を利用して編集ミスを無くすことができる。 | |
// selfという名前の型を定義するのが危なそうな場合はself_にする。 | |
typedef id self; | |
#define typeof_target self | |
#define typeof_weak_target __typeof(self) | |
// Utility | |
#define FPCurrent() NSLog(@"%@ %@", self.class, NSStringFromSelector(_cmd)) | |
#define FPBlocks(block) !(block) ? 0 : (block) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <UIKit/UIKit.h> | |
@interface FPPracticeViewController : UIViewController | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "FPPracticeViewController.h" | |
#import "FPAntiCircularReferenceBlockPractice.h" | |
@interface FPPracticeViewController () | |
@property (nonatomic, strong) FPAntiCircularReferenceBlockPractice *practice; | |
@property (nonatomic, assign) NSInteger runCount; | |
@end | |
@implementation FPPracticeViewController | |
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil | |
{ | |
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; | |
if (self) { | |
[self makePractice]; | |
} | |
return self; | |
} | |
- (void)dealloc | |
{ | |
FPCurrent(); | |
} | |
- (void)makePractice | |
{ | |
self.practice = [[FPAntiCircularReferenceBlockPractice alloc] init]; | |
[self.practice setTarget:self block:^(typeof(self) self) { | |
self.runCount++; | |
[self runPractice]; | |
}]; | |
} | |
- (void)runPractice | |
{ | |
FPCurrent(); | |
NSLog(@"count %@", @(self.runCount)); | |
} | |
- (IBAction)touchedPractice | |
{ | |
[self.practice run]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment