-
-
Save sendoa/335f5b31eacc9cd22c24 to your computer and use it in GitHub Desktop.
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 | |
typedef NSString*(^ConvertBlock)(NSString *text); | |
@interface Thing : NSObject | |
@property (nonatomic, strong) void (^coolPropertyBlock)(NSString *text); // Property | |
// method param | |
- (void)doSomething:(void(^)(NSString *text))block with:(NSString *)person; | |
@end | |
@implementation Thing | |
// method param | |
- (void)doSomething:(void(^)(NSString *text))block with:(NSString *)person { | |
block(person); | |
} | |
@end | |
int main(int argc, char *argv[]) { | |
@autoreleasepool { | |
// Local variable | |
CGFloat (^multiplyBlock)(CGFloat a, CGFloat b) = ^CGFloat(CGFloat a, CGFloat b) { | |
return a * b; | |
}; | |
NSLog(@"2 multiplyBlock 6 => %f", multiplyBlock(2, 6)); | |
Thing *t = [Thing new]; | |
// Property | |
[t setCoolPropertyBlock:^void(NSString *text){ // as argument to a call | |
NSLog(@"%@ is cool!", text); | |
}]; | |
t.coolPropertyBlock(@"Alex"); | |
t.coolPropertyBlock(@"This"); | |
// as argument to a call | |
[t doSomething:^void(NSString *person) { | |
NSLog(@"I'm working with %@", person); | |
} with:@"Robert"]; | |
// typedef | |
ConvertBlock mayus = ^NSString*(NSString *text) { | |
return [text uppercaseString]; | |
}; | |
ConvertBlock minus = ^NSString*(NSString *text) { | |
return [text lowercaseString]; | |
}; | |
NSString *toConvert = @"Hello World!"; | |
NSLog(@"Convert '%@' to '%@'", toConvert, mayus(toConvert)); | |
NSLog(@"Convert '%@' to '%@'", toConvert, minus(toConvert)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment