zoukankan      html  css  js  c++  java
  • What does the “__block” keyword mean?

    It tells the compiler that any variable marked by it must be treated in a special way when it is used inside a block. Normally, variables and their contents that are also used in blocks are copied, thus any modification done to these variables don't show outside the block. When they are marked with __block, the modifications done inside the block are also visible outside of it.

    For an example and more info, see The __block Storage Type in Apple's Blocks Programming Topics.

    The important example is this one:

    extern NSInteger CounterGlobal;
    static NSInteger CounterStatic;
    
    {
        NSInteger localCounter = 42;
        __block char localCharacter;
    
        void (^aBlock)(void) = ^(void) {
            ++CounterGlobal;
            ++CounterStatic;
            CounterGlobal = localCounter; // localCounter fixed at block creation
            localCharacter = 'a'; // sets localCharacter in enclosing scope
        };
    
        ++localCounter; // unseen by the block
        localCharacter = 'b';
    
        aBlock(); // execute the block
        // localCharacter now 'a'
    }

    In this example, both localCounter and localCharacter are modified before the block is called. However, inside the block, only the modification to localCharacter would be visible, thanks to the __block keyword. Conversely, the block can modify localCharacter and this modification is visible outside of the block.

  • 相关阅读:
    记录一下
    Fiddler对谷歌浏览器抓包
    Linux环境部署基本步骤
    JS----this && JS继承
    节流与防抖
    JS---call apply bind的区别&&JS---argument
    浏览器输入url之后到最后网页渲染出来经历了什么
    Bom中的方法
    JS----new和object.create的区别
    有关排序
  • 原文地址:https://www.cnblogs.com/welhzh/p/4362916.html
Copyright © 2011-2022 走看看