zoukankan      html  css  js  c++  java
  • IOS开发(59)之Block Object的调用

    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;
    }

    运行结果

    2013-05-10 06:53:50.893 CallBlockObjectTest[591:303] Trimmed string = Archy

    3 结语

    以上就是所有内容,希望对大家有所帮助。

    Demo下载:http://download.csdn.net/detail/u010013695/5351963

  • 相关阅读:
    如何在Window上使用Git
    【坑】log4j-over-slf4j.jar AND slf4j-log4j12.jar的冲突问题
    如何查看hadoop与hbase的版本匹配关系
    为什么要用Message Queue
    Storm+kafka的HelloWorld初体验
    KafkaOffsetMonitor使用方法
    Linux虚拟机配置本地yum源
    andorid CmakeLists
    python tkinter Treeview 事件绑定
    python我的tkinter学习,玩玩
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3072153.html
Copyright © 2011-2022 走看看