JeOam's Blog

Constructing Block Objects

03 May 2014

Objective-C NSString* (^intToString)(NSUInteger) = ^(NSUInteger paramInteger){ NSString *result = [NSString stringWithFormat:@"%lu", (unsigned long)paramInteger]; return result; };

Objective-C typedef NSString* (^IntToStringConverter)(NSUInteger paramInteger);

```Objective-C - (NSString *) convertIntToString:(NSUInteger)paramInteger usingBlockObject:(IntToStringConverter)paramBlockObject{

return paramBlockObject(paramInteger); } ```

Objective-C - (void) doTheConversion{ NSString *result = [self convertIntToString:123 usingBlockObject:intToString]; NSLog(@"result = %@", result); }

Objective-C - (void) doTheConversion{ IntToStringConverter inlineConverter = ^(NSUInteger paramInteger){ NSString *result = [NSString stringWithFormat:@"%lu", (unsigned long)paramInteger]; return result; }; NSString *result = [self convertIntToString:123 usingBlockObject:inlineConverter]; NSLog(@"result = %@", result); }

```Objective-C - (void) doTheConversion{ NSString *result = [self convertIntToString:123 usingBlockObject:^NSString *(NSUInteger paramInteger) { NSString *result = [NSString stringWithFormat:@”%lu”, (unsigned long)paramInteger]; return result; }];

NSLog(@"result = %@", result); } ```