iOS Block浅析
// 定义一个Block Object,返回值:NSString;别名:intToString;参数:NSUInteger。 NSString* (^intToString)(NSUInteger) = ^(NSUInteger paramInteger){ NSString *result = [NSString stringWithFormat:@"%lu",(unsignedlong)paramInteger]; return result; };
// 调用我们定义的Block Ojbect NSString *string = intToString(10); NSLog(@"string = %@", string);
typedef NSString* (^IntToStringConverter)(NSUInteger paramInteger);
- (NSString *)convertIntToString:(NSUInteger)paramInteger usingBlockObject:(IntToStringConverter)paramBlockObject{ return paramBlockObject(paramInteger); }
NSString *result = [self convertIntToString:123 usingBlockObject:intToString]; NSLog(@"result = %@", result);
typedef NSString* (^IntToStringConverter)(NSUInteger paramInteger);
- (NSString *)convertIntToString:(NSUInteger)paramInteger usingBlockObject:(IntToStringConverter)paramBlockObject{ return paramBlockObject(paramInteger); }
- (void) doTheConversion{ IntToStringConverter inlineConverter = ^(NSUInteger paramInteger){ NSString *result = [NSString stringWithFormat:@"%lu", (unsignedlong)paramInteger]; return result; }; NSString *result = [self convertIntToString:123usingBlockObject:inlineConverter]; NSLog(@"result = %@", result); }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。