欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

IOS开发(59)之Block Object的调用

程序员文章站 2022-06-07 15:00:18
1 前言 本文将介绍如何函数调用block object以及block object调用block object。 2 代码实例 testdemo.h [plain]...

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