通知、block

===================================

通知

===================================

一.通知(NSNotification)

   // NSNotification 通知类,这个类中有 NSNotificationCenter 通知中心类

    NSNotificationCenter* notification = [NSNotificationCenter defaultCenter];

    

    // 添加了一个监听事件,其中,run1 则是触发的事件方法,@“run”是通知的名字

    [notification addObserver:self selector: @selector(run1) name: @"run" object:nil];

    // 多次调用发送通知的方法,会触发多次相应的响应方法(run1)

    [notification postNotificationName: @"run" object:nil];

    // 删除通知,如果想删除通知,就可以调用removeObserver这个方法

    [notification removeObserver:self name:@"run" object:nil];

【注】通知用的时候要添加通知,不用的时候一定要删除通知,因为如果不删除,这个通知一直存在

二.【代理和通知对比】

// 代理:小明->小刚->小红->小李;结果:小李->小红->小刚->小明

// 通知:小明注册了通知;结果:小刚、小红、小李都可以给小明发送消息;

三.通知的注意事项

+ (void)test

{

    xiaoming *xm = [[xiaoming alloc]init];

    [xm test1];

       //【注】不可以在类方法中添加监听方法,这样会导致程序崩溃

//    NSNotificationCenter* notification = [NSNotificationCenter defaultCenter];

//    [notification addObserver:self selector:@selector(run) name:@"run" object:nil];

//    

//    [xiaogang xgTest];

}

- (void)test1

{

    NSNotificationCenter* notification = [NSNotificationCenter defaultCenter];

    [notification addObserver:self selector: @selector(run) name: @"run" object:nil];

    //【注】添加监听事件多次,发送消息时会触发多次run方法

    [notification addObserver:self selector: @selector(run) name: @"run" object:nil];

    [notification addObserver:self selector: @selector(run) name: @"run" object:nil];

    [notification addObserver:self selector: @selector(run) name: @"run" object:nil];

    //【注】删除监听,会删除所有对应的name的监听

     //  [notification removeObserver:self name: @"run" object:nil];

    //【注】删除监听,会删除所有对应的name的监听,object后面的参数应根据addObserver方法中的参数来

    [notification removeObserver:self name: @"run" object: @""];

    [notification addObserver:self selector: @selector(run) name: @"run" object:nil];

    [xiaogang xgTest];

===================================

block

===================================

一.认识block

 block又称为代码块,它是^符号开头的方法;一般用于多线程、网络通信。苹果公司从ios4开始主推block语法

block实体形式如下:

^(传入的参数列表){行为主体(具体的代码实现)}

       // c语言中声明了一个指针函数

        void (* cFunc)(void);

        

        // oc中block 跟指针函数很像

        // 写了一个block变量ocFunc

        void(^ ocFunc)(void);

        

1.不带参数的block

        //【注】block语法,先执行{}外面的语法,只有调用block函数的时候,才会执行内部

        // 实现了一个block函数

        // ^(传入的参数列表){行为主体(具体的代码实现)}

        //【注】block函数是以;结尾

        ocFunc=^(void)

        {

            NSLog(@"in blocks");

        };

        NSLog(@"befor blocks");

        // block函数的调用

        ocFunc();

        NSLog(@"after blocks");

2.带参数的block

        // int 返回值类型;myblock1 block函数名称; int a,int b是形参; ^(int a,int b){};是行为主体

        int (^ myblock1)(int a,int b)=^(int a,int b)

        {

            return a+b;

        };

        // block函数的调用

        int result1 = myblock1(10,20);

        NSLog(@"result1 = %d",result1);

        // 一个函数中无法包含另外一个函数,block应运而生了        

        func(10,20);

        int b = 8;

        int (^myblock2)(int a) = ^(int a)

        {

            return b+a;

        };     

        int result2 = myblock2(5);

        NSLog(@"rusult2 = %d",result2);       

        myBlock myblock3 = ^(int a,int b)

        {

            return a+b;

        };        

        int result3 = myblock3(90,8);

        NSLog(@"rusult3 = %d",result3);

        

        //【注】如果要在block内部对外部变量进行修改,则外部变量需要加__block修饰符(有2条下划线)

        __block int sum;

        void(^myblock4)(int a,int b) = ^(int a,int b)

        {

            sum = a +b;

        };

        myblock4(4,5);

        NSLog(@"sum = %d",sum);

        

        // A这个值会copy一份,block内部的操作是copy的这一部分,所以,外部无论如何对这个A进行修改,block内部都是不变的

        int A = 8;

        int(^myblock5)(int ) = ^(int a)

        {

            return A + a;

        };

        A = 5;

        int result4 = myblock5(3);

        NSLog(@"result4 = %d",result4);

        

        //【注】需要注意的是,这里copy的值是一个变量的值,如果是一个记忆体的位置(地址),也就说,就是这个变量的指针的话,它的值在block内部会被改变        

        NSMutableArray* array = [[NSMutableArray alloc]initWithObjects:@"one",@"two",@"three", nil];

        void(^myblock6)(void) = ^(void)

        {

            [array removeLastObject];

        };

        // 在第0个位置插入字符串@“0”;

        [array insertObject:@"0" atIndex:0];

        

        myblock6();

        NSLog(@"array = %@",array);

        

        // 对sum进行赋值,发现sum值被修改了

        void(^myblock7)(void) = ^(void)

        {

            sum = 6;

        };

        myblock7();

        NSLog(@"sum = %d",sum);

//        static int B = 8;

//        int (^myblock8)(int) = ^ (int a)

//        {

//            return B+a;

//        };

//        B  = 5;

//        int result5 = myblock8(3);

//        NSLog(@"result5 = %d",result5);

        

        static int B = 8;

        int (^myblock8)(int) = ^ (int a)

        {

            B  = 5;

            return B+a;

        };

        int result5 = myblock8(3);

        NSLog(@"result5 = %d",result5);

        // [注]如果想把一个变量参与到block中运算修改,加一个static修饰符即可

原文地址:https://www.cnblogs.com/ljcgood66/p/5334808.html