Skip to content

Instantly share code, notes, and snippets.

@leeprobert
Last active December 16, 2015 04:29
Show Gist options
  • Save leeprobert/5377531 to your computer and use it in GitHub Desktop.
Save leeprobert/5377531 to your computer and use it in GitHub Desktop.
Blocks explorer. Demonstrates Block creation and usage in Objective C. Includes creation of Class methods with blocks as arguments, and creating type definitions for block signatures.
//
// BlockWorker.h
// BlocksExplorer
//
// Created by Lee Probert on 13/04/2013.
// Copyright (c) 2013 Lee Probert. All rights reserved.
//
#import <Foundation/Foundation.h>
typedef BOOL (^BlockWorkerSuccessBlockType)(void);
typedef BOOL (^BlockWorkerFailureBlockType)(void);
typedef int(^blk1_t)(int x);
@interface BlockWorker : NSObject
+(NSString*) generate:(NSArray*)data withBlock:(BOOL (^)(NSString* val))aBlock;
+(void) checkOddNumber:(int)num success:(BlockWorkerSuccessBlockType)successBlock failure:(BlockWorkerFailureBlockType)failureBlock;
+(int) methodUsingBlock:(blk1_t)blk value:(int)v;
+(blk1_t) methodReturningBlock;
@end
//
// BlockWorker.m
// BlocksExplorer
//
// Created by Lee Probert on 13/04/2013.
// Copyright (c) 2013 Lee Probert. All rights reserved.
//
#import "BlockWorker.h"
@implementation BlockWorker
+(NSString*) generate:(NSArray*)data withBlock:(BOOL (^)(NSString* val))aBlock {
NSMutableString *str = [NSMutableString string];
for(NSString *v in data) {
if(aBlock(v)){
[str appendString:v];
}
}
return str;
}
+(void)checkOddNumber:(int)num success:(BlockWorkerSuccessBlockType)successBlock failure:(BlockWorkerFailureBlockType)failureBlock {
if(num%1>0){
successBlock();
}else{
failureBlock();
}
}
+(int) methodUsingBlock:(blk1_t)blk value:(int)v {
/*
Method adjusts the value of v internally and then passes that value back into the callers block
*/
float a = v/10;
return blk(a);
}
+(blk1_t) methodReturningBlock {
/*
The returned block can be passed into the method defined above.
*/
return ^(int x) {
return 100*x;
};
}
@end
//
// main.m
// BlocksExplorer
//
// Created by Lee Probert on 13/04/2013.
// Copyright (c) 2013 Lee Probert. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "BlockWorker.h"
void basicBlockVariable();
void typeDefBlockVariable(NSString* str);
void workerBlockInvocation(NSArray* arr);
void comparatorBlock(NSArray* arr);
void blockCallback(int num);
void usingBlockWithPassedFunctionParameter(int num);
typedef BOOL (^BoolBlockType)(NSString*);
typedef void (^VoidBlockType)(int a, int b);
int main(int argc, const char * argv[])
{
@autoreleasepool {
basicBlockVariable();
typeDefBlockVariable(@"Lee");
workerBlockInvocation(@[@"Lee",@"Lyra",@"Alice",@"Lesley",@"Martha"]);
comparatorBlock(@[@"Lee",@"Lyra",@"Alice",@"Lesley",@"Martha"]);
blockCallback(48);
usingBlockWithPassedFunctionParameter(100);
}
return 0;
}
void basicBlockVariable () {
int multiplier = 7;
int (^myBlock)(int) = ^(int num) {
return num * multiplier;
};
printf("basic Block variable example : %i\n",myBlock(600));
}
void typeDefBlockVariable (NSString* str) {
__block int c = 10;
BoolBlockType bBlock = ^(NSString* v) {
return [v isEqualToString:str];
};
VoidBlockType vBlock = ^(int a, int b) {
c+=a+b;
};
vBlock(20,40); // return value is void but internally it changes the value of c
printf("Result : %i\n",bBlock(@"Lee"));
printf("New value of c variable = %i\n",c);
}
void workerBlockInvocation(NSArray* arr) {
NSString *result = [BlockWorker generate:arr withBlock:^BOOL(NSString * v) {
return [v isEqualToString:@"Lee"];
}];
NSLog(@"Worker block result : %@",result);
__block NSString* name = @"Martha";
BOOL(^myBlock)(NSString*) = ^(NSString* v) {
return [v isEqualToString:name];
};
NSLog(@"Worker block result 2 : %@",[BlockWorker generate:arr withBlock:myBlock]);
name = @"Lesley";
NSLog(@"Worker block result 3 : %@",[BlockWorker generate:arr withBlock:myBlock]);
};
void comparatorBlock (NSArray *arr) {
static NSStringCompareOptions comparisonOptions = NSCaseInsensitiveSearch | NSNumericSearch | NSWidthInsensitiveSearch | NSForcedOrderingSearch;
NSLocale *currentLocale = [NSLocale currentLocale];
NSComparator sortBlock = ^(id string1, id string2) {
NSRange string1Range = NSMakeRange(0, [string1 length]);
return [string1 compare:string2 options:comparisonOptions range:string1Range locale:currentLocale];
};
NSArray *finderSortArray = [arr sortedArrayUsingComparator:sortBlock];
NSLog(@"finderSortArray: %@", finderSortArray);
}
void blockCallback(int num) {
printf("Checking if %i is odd.\n",num);
[BlockWorker checkOddNumber:num success:^BOOL{
NSLog(@"Yes! It was odd!");
return YES;
} failure:^BOOL{
NSLog(@"No. It was even!");
return NO;
}];
}
void usingBlockWithPassedFunctionParameter(int num){
/*
In the example below num is used as the block parameter x
This would be great if you needed to pass a value into a method to be processed internally and also
by a block you can define.
*/
printf("RESULT : %i",[BlockWorker methodUsingBlock:^int(int x) {
return 100*x;
} value:num]);
/*
Here's an example of the same result as above but by also calling a class method to return the block
*/
printf("\nRESULT 2 : %i",[BlockWorker methodUsingBlock:[BlockWorker methodReturningBlock] value:num]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment