Skip to content

Instantly share code, notes, and snippets.

@alexito4
Created July 26, 2014 13:03
Show Gist options
  • Save alexito4/939c15ba95cf3f255453 to your computer and use it in GitHub Desktop.
Save alexito4/939c15ba95cf3f255453 to your computer and use it in GitHub Desktop.
#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