https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Blocks/Articles/bxOverview.html#//apple_ref/doc/uid/TP40007502-CH3-SW1

Conceptual Overview

Block objects provide a way for you to create an ad hoc function body as an expression in C, and C-derived languages such as Objective-C and C++. In other languages and environments, a block object is sometimes also called a “closure”. Here, they are typically referred to colloquially as “blocks”, unless there is scope for confusion with the standard C term for a block of code.

Block Functionality

A block is an anonymous inline collection of code that:

  • Has a typed argument list just like a function

  • Has an inferred or declared return type

  • Can capture state from the lexical scope within which it is defined

  • Can optionally modify the state of the lexical scope

  • Can share the potential for modification with other blocks defined within the same lexical scope

  • Can continue to share and modify state defined within the lexical scope (the stack frame) after the lexical scope (the stack frame) has been destroyed

You can copy a block and even pass it to other threads for deferred execution (or, within its own thread, to a runloop). The compiler and runtime arrange that all variables referenced from the block are preserved for the life of all copies of the block. Although blocks are available to pure C and C++, a block is also always an Objective-C object.

Usage

Blocks represent typically small, self-contained pieces of code. As such, they’re particularly useful as a means of encapsulating units of work that may be executed concurrently, or over items in a collection, or as a callback when another operation has finished.

Blocks are a useful alternative to traditional callback functions for two main reasons:

  1. They allow you to write code at the point of invocation that is executed later in the context of the method implementation.

    Blocks are thus often parameters of framework methods.

  2. They allow access to local variables.

    Rather than using callbacks requiring a data structure that embodies all the contextual information you need to perform an operation, you simply access local variables directly.

block的优势的更多相关文章

  1. Block循环引用问题研究

    自从苹果在objc中添加Block功能支持以后已经过了很久.目前网上对于Block的使用有很多介绍.不过对于Block的内存管理问题,则是众说纷纭.再加上objc开始使用ARC以后,对于Block的内 ...

  2. Hive文件存储格式

    hive文件存储格式 1.textfile textfile为默认格式   存储方式:行存储   磁盘开销大 数据解析开销大   压缩的text文件 hive无法进行合并和拆分 2.sequencef ...

  3. Hive 文件格式

    hive文件存储格式包括以下几类: 1.TEXTFILE 2.SEQUENCEFILE 3.RCFILE 4.ORCFILE(0.11以后出现) 其中TEXTFILE为默认格式,建表时不指定默认为这个 ...

  4. Windows Azure Storage (19) 再谈Azure Block Blob和Page Blob

    <Windows Azure Platform 系列文章目录> 请读者在参考本文之前,预习相关背景知识:Windows Azure Storage (1) Windows Azure St ...

  5. iOS Block简介

    Block是对象,它封装了一段代码,这段代码可以在任何时候执行.block可以作为函数参数或者函数的返回值,而其本身又可以带输入参数或返回值.它和传统的函数指针很类似,但是有区别:block是inli ...

  6. 关于Block的copy和循环引用的问题

    http://blog.csdn.net/felix9/article/details/9619313 在实际开发中,发现使用Block有着比delegate和notification更简洁的优势.于 ...

  7. Block的copy和循环引用的问题

    在实际开发中,发现使用Block有着比delegate和notification更简洁的优势.于是在目前的项目中大量的使用block. 在我的头文件我是这样声明使用block的. 我将block声明为 ...

  8. iOS开发——OC篇&消息传递机制(KVO/NOtification/Block/代理/Target-Action)

     iOS开发中消息传递机制(KVO/NOtification/Block/代理/Target-Action)   今晚看到了一篇好的文章,所以就搬过来了,方便自己以后学习 虽然这一期的主题是关于Fou ...

  9. 黄聪:Microsoft Enterprise Library 5.0 系列教程(七) Exception Handling Application Block

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(七) Exception Handling Application Block 使用企业库异常处理应用程序模块的 ...

随机推荐

  1. ubuntu的 mysql 存储目录迁移

    1:sudo service MySQL stop#迁移前必须先停止mysql 2:创建mysql 存放的 目标文件夹 一般 默认的 mysql 存储目录在  /var/lib中 看清楚  文件的权限 ...

  2. ubuntu16.04安装php5

    系统源自带是7.0的, 如果要安装安装5.5+或者有5.5+的源可以执行这些命令 sudo apt-get install python-software-propertiessudo apt-get ...

  3. UVa 10214 Trees in a Wood. (数论-欧拉函数)

    题意:给定一个abs(x) <= a, abs(y) <= b,除了原点之外的整点各有一棵树,可以相互阻挡,求从原点可以看到多少棵树. 析:由于a < b,所以我们可以一列一列的统计 ...

  4. RXJS组件间超越父子关系的相互通信

    RXJS组件间超越父子关系的相互通信 用到这个的需求是这样的: 组件A有数据变化,将变化的数据流通知组件B接收这个数据流并做相应的变化 实例化RXJS的subject对象 import { Injec ...

  5. HDU3038【种类并查集】

    题意: 给出m组区间[a,b],以及其区间的和,问有矛盾的有几组: 思路: 种类并查集. 主要是几个关系:同类元素的关系,父亲与儿子的关系,不同类元素的关系: 我们可以类似看作一个前缀和,sum[x] ...

  6. lightoj1001【简单题】

    题意: 一个人的值不能超过10: #include<stdio.h> #include<queue> #include<string.h> #include< ...

  7. 阿里云物联网 .NET Core 客户端 | CZGL.AliIoTClient:4. 设备上报属性

    文档目录: 说明 1. 连接阿里云物联网 2. IoT 客户端 3. 订阅Topic与响应Topic 4. 设备上报属性 4.1 上报位置信息 5. 设置设备属性 6. 设备事件上报 7. 服务调用 ...

  8. 自然语言处理(三)——PTB数据的batching方法

    参考书 <TensorFlow:实战Google深度学习框架>(第2版) 从文本文件中读取数据,并按照下面介绍的方案将数据整理成batch. 方法是:先将整个文档切分成若干连续段落,再让b ...

  9. 受保护的封装 protected

    补充内容:封装 私有化封装 private受保护的封装 protected公共的封装 public 注意:python目前不支持设定受保护的成员,但是开发者由约定的使用方式 受保护的封装:在成员名称前 ...

  10. MVC3+jquery Uploadify 上传文件

    最近做项目用到了上传图片的功能,以前也写过这类代码,不过都是用传统的file标签,今天整理一个好用的插件Uploadify..都做了一些注释,一看便知. 可以去官网下载最新的:Uploadify下载地 ...