Created
February 17, 2014 11:54
-
-
Save neiraza/9049288 to your computer and use it in GitHub Desktop.
blockでコールバックを書く ref: http://qiita.com/neiraza/items/e54947e435751d965a2b
This file contains hidden or 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> | |
typedef void (^BlockSampleCallBack)(NSArray *foo); | |
@interface BlockSample : NSObject | |
- (void)hoge:(NSString *)fuga callback:(BlockSampleCallBack)callback; | |
@end |
This file contains hidden or 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 "BlockSample.h" | |
@implementation BlockSample | |
- (void)hoge:(NSString *)fuga callback:(BlockSampleCallBack)callback | |
{ | |
NSArray *array = @[@"りんご", @"ごりら", @"らぶらぶ"]; | |
if (callback) { | |
callback(array); | |
} | |
} | |
@end |
This file contains hidden or 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
// いろいろと割愛 | |
- (void)moDMP | |
{ | |
// BlockSampleがシングルトンだったとして... | |
[[BlockSample] sharedManager] hoge:@"これはテストだ" callback:^(NSArray *foo) { | |
NSLog(@"アホな配列がやってくるお %@", foo); | |
}]; | |
// って書く人もいるけど | |
[[BlockSample] sharedManager] hoge:@"これはテストだ" callback:^(NSArray *foo) {}]; | |
// 僕はnilを入れるので、BlockSample.m内でcallbackを使う際にnil対策したった | |
[[BlockSample] sharedManager] hoge:@"これはテストだ" callback:nil]; | |
} | |
// いろいろと割愛 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment