首页 > 其他 > 详细

Object C 的 blocks 练习

时间:2015-09-23 21:06:10      阅读:227      评论:0      收藏:0      [点我收藏+]

blocks 这东西就是个函数指针和匿名函数。

示例代码 

//
//  main.m
//  CompletionBlock
//
//  Created by liubing on 15/9/23.
//  Copyright © 2015年 QuentinLabs. All rights reserved.
//

#import <Foundation/Foundation.h>

typedef void(^CompletionBlock)();
typedef void(^CompletionBlockInt)(int);



@interface SampleClass : NSObject

-(void)performActionWithCompletion:(CompletionBlock)completionBlock;

-(void)performActionWithCompletionInt:(CompletionBlockInt)completionBlockInt withInt:(int) argInt;

@end


@implementation SampleClass

-(void) performActionWithCompletion:(CompletionBlock)completionBlock
{
    NSLog(@"Action Performed");
    completionBlock();
}

-(void)performActionWithCompletionInt:(CompletionBlockInt)completionBlockInt withInt:(int) argInt;
{
    NSLog(@"Action Performed ");
    completionBlockInt(argInt);
    
}
@end


int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...
        // NSLog(@"Hello, World!");
        
        SampleClass* sampleClass = [[SampleClass alloc]init];
        [sampleClass performActionWithCompletion:^
        {
            NSLog(@"Completion is called to intimate action is performed.");
        }];
        
        [sampleClass performActionWithCompletionInt:
         ^(int argValue)
         {
            NSLog(@"Completion is Called to %i",argValue);
         }
         withInt:1024];
    }
    
    return 0;
}

 

10.10.5 + XCode 7 下编译通过

教程地址 http://www.tutorialspoint.com/objective_c/objective_c_blocks.htm

注: http://www.tutorialspoint.com  是个蛮不错的教程网站。

Object C 的 blocks 练习

原文:http://www.cnblogs.com/lbfamous/p/4833280.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!