IOS开发(59)之Block Object的调用
1 前言
本文将介绍如何函数调用block object以及block object调用block object。
2 代码实例
testdemo.h
[plain]
#import <foundation/foundation.h>
@interface testdemo : nsobject
- (void) callsimpleblock;
- (void) calltrimblock;
@end
#import <foundation/foundation.h>
@interface testdemo : nsobject
- (void) callsimpleblock;
- (void) calltrimblock;
@end
testdemo.m
[plain]
#import "testdemo.h"
@implementation testdemo
/*************** 方法调用block object start ***************/
void (^simpleblock)(nsstring *) = ^(nsstring *paramstring){
/* implement the block object here and use the paramstring parameter */
nslog(@"%@",paramstring);
};
- (void) callsimpleblock{
simpleblock(@"archy");
}
/*************** 方法调用block object end ***************/
/*************** block object调用block object start ***************/
nsstring *(^trimstring)(nsstring *) = ^(nsstring *inputstring){
nsstring *result = [inputstring stringbytrimmingcharactersinset:[nscharacterset whitespacecharacterset]];
return result;
};
nsstring *(^trimwithotherblock)(nsstring *) = ^(nsstring *inputstring){
return trimstring(inputstring);
};
- (void) calltrimblock{
nsstring *trimmedstring = trimwithotherblock(@" archy ");
nslog(@"trimmed string = %@", trimmedstring);
}
/*************** block object调用block object start ***************/
@end
#import "testdemo.h"
@implementation testdemo
/*************** 方法调用block object start ***************/
void (^simpleblock)(nsstring *) = ^(nsstring *paramstring){
/* implement the block object here and use the paramstring parameter */
nslog(@"%@",paramstring);
};
- (void) callsimpleblock{
simpleblock(@"archy");
}
/*************** 方法调用block object end ***************/
/*************** block object调用block object start ***************/
nsstring *(^trimstring)(nsstring *) = ^(nsstring *inputstring){
nsstring *result = [inputstring stringbytrimmingcharactersinset:[nscharacterset whitespacecharacterset]];
return result;
};
nsstring *(^trimwithotherblock)(nsstring *) = ^(nsstring *inputstring){
return trimstring(inputstring);
};
- (void) calltrimblock{
nsstring *trimmedstring = trimwithotherblock(@" archy ");
nslog(@"trimmed string = %@", trimmedstring);
}
/*************** block object调用block object start ***************/
@end
main.m
[plain] view plaincopyprint?int main(int argc, const char * argv[])
{
@autoreleasepool {
testdemo *test = [[testdemo alloc] init];
// [test callsimpleblock];
[test calltrimblock];
}
return 0;
}
int main(int argc, const char * argv[])
{
@autoreleasepool {
testdemo *test = [[testdemo alloc] init];
// [test callsimpleblock];
[test calltrimblock];
}
return 0;
}
运行结果
2013-05-10 06:53:50.893 callblockobjecttest[591:303] trimmed string = archy
上一篇: 浅析十款PHP开发框架的对比
下一篇: 图片水印添加专家使用教程