你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> IOS開發(59)之Block Object的調用

IOS開發(59)之Block Object的調用

編輯:IOS開發綜合

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

 

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved