码迷,mamicode.com
首页 > 其他好文 > 详细

Object C 的 blocks 练习

时间:2015-09-23 21:07:01      阅读:145      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!