Objective-C中Block的类型

作者: hanl001 | 来源:发表于2016-12-06 16:11 被阅读133次

    在研究block的类型之前,首先我们要知道c类语言的程序编译后在内存中的分布(由高位向低位排序):

    1. 栈区:局部变量
    2. 堆区:由"alloc"开辟的空间就是在堆区
    3. 静态区(全局区): 静态变量、 全局变量(注:未初始化的在bss)
    4. 常量区: 常量
    5. 代码区: 二进制代码段

    这里我们将MRC和ARC分开讨论:


    1、在MRC下有三种类型的block
    • NSGlobalBlock: 位于内存全局区、不调用外部变量或者仅调用静态区常量区的"变量"
    CGFloat gFloat = 1.2;
    NSString *gStr = @"全局字符串";
    @implementation test  
    -(void)globalTest { 
        //注:在本例中NSSting分别在内存中的哪个区域呢?
        //1、不调用外部变量的block
        void(^gblock1)(NSString *) = ^(NSString *str){ 
             NSLog(@"%@",str);
         };
        NSLog(@"%@",gblock1);
        gblock1(@"传入字符串常量");
        //2、仅调用全局变量的block
        void(^gblock2)(void) = ^{ 
          NSLog(@"%lf",gFloat);
        };
        NSLog(@"%@",gblock2);
        gblock2();
        void(^gblock3)(void) = ^{ 
          NSLog(@"%@",gStr);
        };
        NSLog(@"%@",gblock3);
        gblock3();
       //3、仅调用静态变量的block
        static CGFloat sFloat = 2.1;
        void(^gblock4)(void) = ^{
          NSLog(@"%lf",sFloat);
        };
        NSLog(@"%@",gblock4);
        gblock4();
        static NSString *staStr = @"静态字符串";
        void(^gblock5)(void) = ^{
          NSLog(@"%@",staStr);
        };
        NSLog(@"%@",gblock5);
        gblock5();
    }```
    * 控制台输出:
    

    2016-12-06 14:50:22.928972 test_demo[18845:866060] <NSGlobalBlock: 0x100001050>
    2016-12-06 14:50:22.929176 test_demo[18845:866060] 传入字符串常量
    2016-12-06 14:50:22.929221 test_demo[18845:866060] <NSGlobalBlock: 0x100001090>
    2016-12-06 14:50:22.929246 test_demo[18845:866060] 1.200000
    2016-12-06 14:50:22.929268 test_demo[18845:866060] <NSGlobalBlock: 0x1000010d0>
    2016-12-06 14:50:22.929288 test_demo[18845:866060] 全局字符串
    2016-12-06 14:50:22.929307 test_demo[18845:866060] <NSGlobalBlock: 0x100001110>
    2016-12-06 14:50:22.929320 test_demo[18845:866060] 2.100000
    2016-12-06 14:50:22.929338 test_demo[18845:866060] <NSGlobalBlock: 0x100001150>
    2016-12-06 14:50:22.929354 test_demo[18845:866060] 静态字符串```
    </br>

    • NSStackBlock: 位于内存栈区、仅调用栈区变量
    -(void)stackTest {
        //局部变量
        CGFloat sFloat = 1.1;
        void(^sBlock1)(void) = ^{
          NSLog(@"%lf",sFloat);
        };
        NSLog(@"%@",sBlock1);
        sBlock1();
    }
    
    • 控制台输出:
    2016-12-06 15:04:04.606908 test_demo[18880:873406] <__NSStackBlock__: 0x7fff5fbff6d8>
    2016-12-06 15:04:04.607076 test_demo[18880:873406] 1.100000```
    *   **`NSMallocBlock:`** 位于内存堆区、由栈区copy到堆区
    <pre> -(void)mallocTest {
        //局部变量
        CGFloat sFloat = 1.1;</br>
        void(^sBlock1)(void) = ^{</br>        NSLog(@"%lf",sFloat);
        };
     //    NSLog(@"%@",sBlock1);
    //    sBlock1();</br>
        void(^mBlock1)(void) = [sBlock1 copy];
        NSLog(@"%@",mBlock1);</br>
        //@property(copy,nonatomic)void(^mBlock2)(void);
        self.mBlock2 = sBlock1;
        NSLog(@"%@",self.mBlock2);
    }</pre>
    * `控制台输出:`
    

    2016-12-06 15:37:07.306385 test_demo[28582:914325] <NSMallocBlock: 0x100402f00>
    2016-12-06 15:37:07.306699 test_demo[28582:914325] <NSMallocBlock: 0x100600000>

    ***
    ######2、在ARC下,仅存在 `NSGlobalBlock ` 、`NSMallocBlock` 两种block
    </br>我们切换到arc环境,并且将 `mallocTest` 中的如下代码注释取消
    

    // NSLog(@"%@",sBlock1);
    // sBlock1();```
    运行得到输出结果:

    2016-12-06 15:52:42.884091 test_demo[35541:942156] <__NSMallocBlock__: 0x100502eb0>
    2016-12-06 15:52:42.884379 test_demo[35541:942156] 1.100000
    2016-12-06 15:52:42.884416 test_demo[35541:942156] <__NSMallocBlock__: 0x100502eb0>
    2016-12-06 15:52:42.884446 test_demo[35541:942156] <__NSMallocBlock__: 0x100502eb0>
    

    由此可以判断,在arc下没有 NSStackBlock 类型变量、并且 copy 操作只是对堆区 block 进行了一次引用,既然如此我接着对 NSGlobalBlockNSMallocBlock 进行copy操作:

        static NSString *staStr = @"静态字符串";
        void(^gblock5)(void) = ^{
            NSLog(@"%@",staStr);
        };
        NSLog(@"%@",gblock5);
        gblock5();
        void(^copyBlock)(void) = [gblock5 copy];
        NSLog(@"%@",copyBlock);```
    * `控制台输出:`
    

    2016-12-06 16:24:36.832399 test_demo[35671:957646] <NSGlobalBlock: 0x100002170>
    2016-12-06 16:24:36.832415 test_demo[35671:957646] 静态字符串
    2016-12-06 16:24:36.832438 test_demo[35671:957646] <NSGlobalBlock: 0x100002170>

    
    
    void(^mBlock1)(void) = [sBlock1 copy];
    NSLog(@"%@",mBlock1);
    void(^copyBlock)(void) = [mBlock1 copy];
    NSLog(@"%@",copyBlock);```
    
    • 控制台输出:
    2016-12-06 16:27:10.688490 test_demo[35702:959578] <__NSMallocBlock__: 0x1006000c0>
    2016-12-06 16:27:10.688571 test_demo[35702:959578] <__NSMallocBlock__: 0x1006000c0>```
    ***
    ##### 总结:
    1. 当block中没有引用任何外部变量时或者仅仅调用静态区变量时,编译器直接将block放在静态区(减少堆区的占用有利于性能的)
    2. copy 操作对将NSStackBlock拷贝到堆区、对另外两种类型只是强引用
    3. arc下是没有NSStackBlock类型block的(block被当做对象处理)
    
    附[测试代码](https://github.com/hanl001/blockType_test)

    相关文章

      网友评论

        本文标题:Objective-C中Block的类型

        本文链接:https://www.haomeiwen.com/subject/xfxlmttx.html