objective-c – 块如何捕获其封闭范围之外的变量?
发布时间:2020-12-16 05:30:09 所属栏目:百科 来源:网络整理
导读:我知道一个Objective-C块可以捕获和设置变量的值在其封闭范围之外.这是怎么做到的 解决方法 这实际上相当简单,并在 “Imported Variables”部分的Clang的Block Implementation Spec中进行了描述. 当编译器遇到一个Block,就像: ^{ if( numBalloons numClowns
我知道一个Objective-C块可以捕获和设置变量的值在其封闭范围之外.这是怎么做到的
解决方法
这实际上相当简单,并在
“Imported Variables”部分的Clang的Block Implementation Spec中进行了描述.
当编译器遇到一个Block,就像: ^{ if( numBalloons > numClowns) abort(); } 它创建了一个文字结构,其中包括 – 其中包括两个重要的元素.有一个指向Block中的可执行代码的函数指针,以及Block中引用的每个变量的const字段.这样的东西 struct __block_literal_1 { /* other fields */ void (*invoke)(struct __block_literal_1 *); /* ... */ const int numBalloons; const int numClowns; }; 请注意,invoke函数将指向在此定义的类型的结构体的指针;也就是说,Block在执行代码时自动执行.因此,代码可以访问结构的成员. 在声明之后,编译器创建一个Block的定义,它只是使用引用的变量来初始化struct中的正确字段: struct __block_literal_1 __block_literal_1 = { /* Other fields */ __block_invoke_2,/* This function was also created by the compiler. */ /* ... */ numBalloons,/* These two are the exact same variables as */ numClowns /* those referred to in the Block literal that you wrote. * }; 然后,在invoke函数内部,引用捕获的变量就像struct的其他成员,即__block-> numBalloons一样. 对象类型变量的情况有点复杂,但同样的原则也适用. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |