OC-Block使用

block是一种非常方便的回调方法,也是苹果官方所推荐的使用方法,下面是我对block使用的一点心得。

上代码:

先创建一个CAR类:

#import <Foundation/Foundation.h>


@interface CAR : NSObject



- (void)doPrintCar:(void (^)(void))actionHandler ;

- (void)carUsingBlock:(void (^)(NSUInteger idx, NSString *a))block;

-(void)docarSomeThing;

@property (nonatomic, copy) void (^printCarAction)(void);

@property (nonatomic, copy) void (^daicanshu)(NSUInteger idx, NSString *a);

@end



#import "CAR.h"


@implementation CAR

@synthesize printCarAction;

@synthesize daicanshu;

- (void)doPrintCar:(void (^)(void))actionHandler{

    NSLog(@"执行了CAR类的doPrintCar 方法了");

    self.printCarAction = actionHandler;//把参数中block的代码全部传过来

    NSLog(@"等待三秒执行回调");

    [NSTimer scheduledTimerWithTimeInterval:3.0f target:self selector:@selector(doSomeThing) userInfo:nil repeats:NO];

}

- (void)carUsingBlock:(void (^)(NSUInteger idx, NSString *a))block {

    self.daicanshu = block;

    [self docarSomeThing];

}

-(void)docarSomeThing{

    daicanshu(1,@"honda");

}

-(void)doSomeThing{

    //回调回去

    NSLog(@"回调回去!!");

    printCarAction();

}

@end




然后在Viewcontroller中:

#import "ViewController.h"


@interface ViewController ()


@end



@implementation ViewController


int price;


int (^isInputEven)(int) = ^(int input){

    input = input + price;

    return input;

};


typedef int(^oisInputEven)(int);


 oisInputEven  logcount = ^(int oinput){

    oinput = oinput + 11;

    return oinput;

};



- (void)viewDidLoad

{

    [super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

    

    price = 12;

    NSLog(@"isInputEven = %d ", isInputEven(10));

    NSLog(@"logcount = %d",logcount(10));


    

    

    // Create an array

    NSArray *array = [NSArray arrayWithObjects: @"12345", @"12345678", @"abcd", @"123abc", nil];

    

    /*作为参数传block

    iOS框架内的数量,方法中所提供接受一个block作为参数。因为它涉及到block,这是一个事情开始变得有趣了,你会得到一个角度上提供了一些独特的功能。

    

    例如,下面的代码中,我创建了一个String对象的数组,没有什么新东西。然而,在iOS 4 + NSArray中现在包括一个方法来创建一个集合(NSSet)对象的数组,通过一个indexesOfObjectsPassingTest方法。我写检查的block长度的数组中的每个对象(每个对象是在这个例子中的字符串),并返回YES,只对那些对象的字符串长度大于5*/

    

    NSIndexSet *set = [array indexesOfObjectsPassingTest: ^(id obj, NSUInteger idx, BOOL *stop)

    {

        if ([obj length] > 5)

            return YES;

        else

            return NO;

    }];

    

    // 现在,我们有一个集合,它告诉我们哪些对象是一个指定的长度(或更大),让我们来调用一个方法在NSSet对象中,将枚举的值的集合和打印的有关阵列条目的值-的方法enumerateIndexesUsingBlock在,NSSet会也接受block作为参数:

    

    [set enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop)

    {

        NSLog(@"Array entry %d contains: %@", idx, [array objectAtIndex:idx]);

    }];

    

    //调用其他地方的block 内联函数 作参数

    CAR *honda = [[CAR alloc]init];

    [honda doPrintCar:^{

        NSLog(@"回调成功,这是一辆本田汽车");

    }];

    

    [honda carUsingBlock:^(NSUInteger idx,NSString *a)

    {

        NSLog(@"回调回来的参数是 %d\n%@",idx,a);

    }];

}

@end



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值