Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#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
You can’t perform that action at this time.