ios - 我很难理解苹果 block 编程的这一部分

标签 ios

<分区>

这是来自 apple block programming , 谁能告诉我这是什么意思

Stack (non-static) variables local to the enclosing lexical scope are captured as const variables.

最佳答案

假设你有:

int i = 5; // in stack

然后在 block 中你有:

...
i++; // can't do that, because i now inside the block is a const
...

您要将 __block 添加到 i 声明,以便能够像这样在 block 内更改 i 的值:

__block int i = 5; // remove __block and see the error

void (^myBlock)(void) = ^{
    NSLog(@"[inside block] i = %i", i); // no error even without __block
    i++; // error here without __block
};

myBlock();

NSLog(@"[outside block] i = %i", i);

关于ios - 我很难理解苹果 block 编程的这一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13064713/

上一篇:iphone - 缩放时,UIScrollview subview 无法识别手势

下一篇:objective-c - 在 nsuserdefaults 中设置值

相关文章:

iphone - 释放UIView会自动调用removeFromSuperView吗?

ios - 为什么将我的对象添加到NSMutableArray无法正常工作?

ios - "cellForRowAtIndexPath"未调用 alertviewcontroller 内的 TableView

ios - NSInvocation 和内存问题

ios - 如何在 ios,swift 中使用 RSA 公钥加密和解密字符串(纯文本)

ios - UITableView 在上方和下方添加空行,在将页眉和页脚设置为空 View 后仍然存在

ios - 如何在单击“取消”按钮时退出搜索?

ios - 获取完整的相机 View

iphone - Autolayout 的尾随空格问题

ios - 根据按钮选择状态将无法识别的选择器发送到实例