您的位置:首页 > 移动开发 > Objective-C

Block Object

2013-10-21 10:20 405 查看

1 前言

本文将介绍如何函数调用Block Object以及Block Object调用Block Object。

2 代码实例

TestDemo.h

#import <Foundation/Foundation.h>

@interface TestDemo : NSObject

- (void) callSimpleBlock;
- (void) callTrimBlock;
@end
TestDemo.m

#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

int main(int argc, const char * argv[])
{

@autoreleasepool {

TestDemo *test = [[TestDemo alloc] init];
//        [test callSimpleBlock];
[test callTrimBlock];
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Object interface import