block

来源:互联网 发布:复杂网络定义 编辑:程序博客网 时间:2024/06/17 02:31
  • 把零碎的块(block)清理掉.

//定义blockvoid (^printTheNum)(int) = ^(int num){    NSLog(@"%d",num);};- (void)viewDidLoad {    [super viewDidLoad];//    1.    printTheNum(5);//这里输出5

1.这里为什么会输出5呢?这就简单了,这里block相当于调用了函数而已

//    2.    int a=5;    int (^test2)(int) = ^(int a ){        a = a+3;        return a+3;    };    a=20;    NSLog(@"%d",test2(a));//这里是26

2.有读者就想问,这里不是应该报错的吗?报你个头,定义的的block中的参数是a,那么在这个block里,将把20传到参数里面,再进行运算.

//    3.    int b=5;    int (^test3)(int) = ^(int temp ){        return temp+b;    };    b=20;    NSLog(@"%d",test3(3));//这里是8

3.在这里,定义block的时候,就将b的值copy了一份,所以return temp+b;实际上是return temp+5;后面对b的改动是没有影响的.

//    4.//    int x = 100;//    void (^test4)(int) = ^(int y){//        x = x+y;//Variable is not assignable (missing __block type specifier)//        printf("The x value is %d",x);//    };//    test4(50);

4.在这个block里面,就真的会报错了,因为x在block中是不允许改变的.类似于左值.

//    5.    int __block x = 100;    void (^test5)(int) = ^(int y){        x = x+y;        printf("The x value is %d",x);    };    test5(50);//这里是150}

5.在这边加了__block修饰符,就可以改变x的值了.(我就喜欢改,哈!哈!哈!)

一般来说我们也不会在一个类里面调用刚写好的block,这还不如写个函数,直接调调就好了.更加和谐统一.如果真的将block这么用就真的是大材小用了.

  • 把块(block)劈开,看看有没有装什么好东西.

通常我们将block用在两个类之间的交流和通信.


  • 1.情景:用户进到主界面后,想要用一些核心功能.那么肯定要用户登录吧(不能白给你用啊).那么就要求他先登录.跳转到登录界面,那么登录完以后在主界面也要显示用户的称呼,以示我对你尊敬嘛.那这要怎么做呢?
    VC1 --> VC2(输入name) -->VC1 (显示name) VC可理解为页面

VC1中的代码 :

LoginViewController *login = [[LoginViewController alloc] initWithResultBlock:^(BOOL isLogin) {        if (isLogin)        {//登录完成以后再调用的代码.            NSString * heheda = [[publicValue shareValue].userInof objectForKey:@"account"];            [self.my_btn setTitle:heheda forState:UIControlStateNormal];        }    } Animation:YES];    [self presentViewController:login animated:YES completion:^{    }];

VC2中的代码:

@interface LoginViewController : UIViewControllervoid(^getLoginResultBlock)(BOOL isLogin);@end-(void)xixi{    [[publicValue shareValue].userInof setValue:@"ozx" forKey:@"account"];     [publicValue shareValue].isLogin = YES;//调用block     getLoginResultBlock([publicValue shareValue].isLogin);     [self dismissViewControllerAnimated:YES completion:^{     }];}

其实在VC2中, 执行完getLoginResultBlock([publicValue shareValue].isLogin)这句代码以后,就会调用VC1中的代码.

if (isLogin){//登录完成以后再调用的代码.     NSString * heheda = [[publicValue shareValue].userInof objectForKey:@"account"];     [self.my_btn setTitle:heheda forState:UIControlStateNormal];}

  • 2.一个例子不够,那么再来一个
- (void)endInput:(void (^)())hehe{    [UIView animateWithDuration:0.25 animations:^{    } completion:^(BOOL finished) {        if (hehe) {            hehe();        }    }];}//调用[self endInput:^{    NSLog("又要改需求?我擦!");}];

这个例子又有没有看懂?没看懂没关系,我们换种方式再看看

void (^ hehe) = ^{ NSLog("又要改需求?我擦!");};[self endInput:hehe];- (void)endInput:(void (^)())hehe{    [UIView animateWithDuration:0.25 animations:^{    } completion:^(BOOL finished) {        if (hehe) {   //问hehe这个block有没有,有,那么做下面的            hehe();   //hehe()即等同于NSLog("又要改需求?我擦!");        }    }];}

应该可以看懂了把


有很多同学都问,NSArray的那个遍历方法又是怎么回事啊?其实里面也是有进行回调,只是jobs叫下面的人屏蔽了方法而已了啦.
[array enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id str,NSUInteger index, BOOL* te){}]

其实要理解block,首先要灌入一种思想,就是调用时的block如:
[self endInput:^{ NSLog("又要改需求?我擦!");}];
在这一句结束的时候,block里面的代码并没有执行,要等到调用的方法执行了hehe();时才会调用block中的方法.

比如我打你一巴掌(发送消息),然后你说你打到我了(响应消息),那我心里就爽了(回调block).

这就是核心思想.

0 0
原创粉丝点击