JeOam's Blog

Constructing Block Objects

03 May 2014

NSString* (^intToString)(NSUInteger) = ^(NSUInteger paramInteger){
    NSString *result = [NSString stringWithFormat:@"%lu",
                        (unsigned long)paramInteger];
    return result; 
};
typedef NSString* (^IntToStringConverter)(NSUInteger paramInteger);
- (NSString *) convertIntToString:(NSUInteger)paramInteger usingBlockObject:(IntToStringConverter)paramBlockObject{

    return paramBlockObject(paramInteger); }
- (void) doTheConversion{
    NSString *result = [self convertIntToString:123
                               usingBlockObject:intToString];
    NSLog(@"result = %@", result);
}
- (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);
}
- (void) doTheConversion{
        NSString *result =
        [self convertIntToString:123
                usingBlockObject:^NSString *(NSUInteger paramInteger) {
                    NSString *result = [NSString stringWithFormat:@"%lu",
                                        (unsigned long)paramInteger];
                    return result; 
                }];

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