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 = ;
__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.
What does the “__block” keyword mean?的更多相关文章
- ARC下block使用情况
ARC与MRC的block有着一些区别,笔记整理ARC的block,仅仅是自己参考的笔记,详情请参考 http://www.cnbluebox.com/?p=255 在开始之前,请新建一个Model类 ...
- How do I avoid capturing self in blocks when implementing an API?
Short answer Instead of accessing self directly, you should access it indirectly, from a reference t ...
- __Block与__Weak区别
一.__block理解: Blocks可以访问局部变量,但是不能修改, 声明block的时候实际上是把当时的临时变量又复制了一份, 在block里即使修改了这些复制的变量,也不影响外面的原始变量.即所 ...
- __weak与__block区别,深层理解两者区别
准备工作 首先我定义了一个类 MyObject 继承 NSObject,并添加了一个属性 text,重写了description方法,返回 text 的值.这个主要是因为编译器本身对 NSString ...
- MRC迁移ARC之__block
今日帮着同事把老项目从MRC迁移至ARC,大部分工作无非是删除release,[super dealloc]等方法,只要关闭了MRC编译选项后,编译器能自动帮你检查,block就有一些不一样了,发现许 ...
- C#+无unsafe的非托管大数组(large unmanaged array in c# without 'unsafe' keyword)
C#+无unsafe的非托管大数组(large unmanaged array in c# without 'unsafe' keyword) +BIT祝威+悄悄在此留下版了个权的信息说: C#申请一 ...
- __weak 和 __block 区别
Blocks理解: Blocks可以访问局部变量,但是不能修改 如果修改局部变量,需要加__block __block int multiplier = 7; int (^myBlock)(int) ...
- iOS __block 与 __weak
关于__block 与__weak http://stackoverflow.com/questions/19227982/using-block-and-weak 一下是一些区别的介绍 MRC: _ ...
- __block 和 __weak的区别
Blocks理解: Blocks可以访问局部变量,但是不能修改 如果修改局部变量,需要加__block __block int multiplier = 7; int (^myBlock)(int) ...
随机推荐
- Jquery中去除左右空格
$.trim(" abc "); jQuery.trim(" abc ");
- Python3 基础
Hello world 在文本编辑器中,键入python执行代码,保存文件为hello.py print('hello, world') 注意print前面不要有任何空格.然后,选择一个目录,例如C: ...
- javascript 写状态模式
写了状态模式的切换,以及分支循环.but 怎么实现子状态嵌套呢? /** * by JackChen 2016-3-26 11.51.20 * * 状态模式: * 一个状态到另一个状态的变换.其实可以 ...
- 强大的字符串格式化函数 - format
自python2.6开始,新增了一种格式化字符串的函数str.format(),它通过{}和:来代替% 位置方法格式化 >>>'{}-{}'.format('simon','ting ...
- 此文件时入口文件index.php
此文件时入口文件index.php <?php //定义一下ThinkPHP框架存放的路径 define('THINK_PATH','./ThinkPHP/'); //定义当前的项目的名称,此处 ...
- Flume用来收集日志,zeppelin用来展示
Flume:Flume是一个分布式,可依赖的,用于高效率的收集.聚类.移动大量数据的服务.Flume使用基于流数据的简单而且可扩展的架构.由于拥有可调的依赖机制和许多故障恢复机制,Flume是健壮而且 ...
- UM_第三方登录
参考官方文档(http://dev.umeng.com/social/ios/detail-share#7), 做出以下总结. 第三方登录主要用于简化用户登录流程,通过用户拥有的微博.QQ.微信等第三 ...
- struts2简介
MVC框架.不过有一点需要注意的是:struts2和struts2虽然名字很相似,但是在两者在代码编写风格上几乎是不一样的.那么既然有了struts1,为什么还要推出struts2.主要的原因是str ...
- python面向对象编程实例解析
1. 类和函数 面向对象编程的例子: #!/usr/bin/env python # -*- coding: utf-8 -*- class Person(object): #在属性和变量的前面增加“ ...
- Z-stack之OSAL初始化流程
转自点击打开链接 我使用的协议栈版本及例子信息: ZigBee2006\Texas Instruments\ZStack-1.4.3-1.2.1\Projects\zstack\Samples\Sam ...