码迷,mamicode.com
首页 > 移动开发 > 详细

IOS_Block_使用

时间:2015-01-18 18:36:34      阅读:249      评论:0      收藏:0      [点我收藏+]

标签:

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        //first
        void (^myBlock)() = ^{
            NSLog(@"myBlock execute");
        };
        myBlock();
        
        //second return value
        int (^sum)(int, int) = ^int(int x, int y){
            return x+y;
        };
        NSLog(@"sum = %d", sum(2, 6));
        
        //third
        Calc *c = [[Calc alloc] init];
        NSLog(@"s = %d", [c calcNum1:10 withNum2:20 andCalcWith:sum]);

        NSLog(@"value = %d", [c calcNum1:10 withNum2:30 andCalcWith:^int(int x, int y) {
            return x*y;
        }]);
        
    }
    return 0;
}
//
//  Calc.h
//

#import <Foundation/Foundation.h>

typedef int (^calcBlock)(int, int);

@interface Calc : NSObject

-(int) calcNum1:(int)num1 withNum2:(int)num2 andCalcWith:(calcBlock)calc;

@end
//
//  Calc.m
//

#import "Calc.h"

@implementation Calc

- (int)calcNum1:(int)num1 withNum2:(int)num2 andCalcWith:(calcBlock)calc
{
    return calc(num1, num2);
}

@end



IOS_Block_使用

标签:

原文地址:http://blog.csdn.net/zimo2013/article/details/42836705

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!