博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
(译文)IOS block编程指南 3 概念总览
阅读量:5290 次
发布时间:2019-06-14

本文共 2788 字,大约阅读时间需要 9 分钟。

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对象给你提供了创建C语言和C语言拓展语言,比如objective-C和C++中的 ad hoc函数的可能。在其他语言环境中,block对象有时也被称作“closure”,这里呢,通常用大白话称为:“block”,除非作用域中有复杂的标准C block代码。

Block Functionality(block 功能)

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.

一个block是一个匿名内联代码集:

 

  • 像函数一样有一个类型参数列表。
  • 有一种推断或声明的返回值类型
  • 可以在它定义的词法范围内捕获状态
  • 可以随意的在词法作用域里面修改状态
  • 可以和相同作用域的block分享修改
  • 可以在词法作用域(栈)被销毁后继续分享和修改词法作用域中的状态。
你可以复制一个block甚至将它延期到其他线程执行(或者在自己的进程中加入一个循环队列)。编译器和运行时安排所有block的相关变量保存在所有block的拷贝的生命周期中。尽管block可以在纯C和C++中使用,在objective-C中也经常使用block对象。

 

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通常表现为小的独立的代码块。因此呢,block经常被用于作为一个可能同时执行的封装单元,或者集合中的项,或者当操作结束时候的回调。
blocks 因为如下两个原因成为回调函数的一个有效替代者:
  1. 他们允许你在调用点(函数内容已经结束啦)之后写代码。因此呢block常常作为框架函数的参数出现。
  2. 他们允许你访问本地变量。相比于回调函数要求操作的所有上下文信息,block只需要直接访问本地变量就行了。
本文原创,转载请注明出处:http://blog.csdn.net/zhenggaoxing/article/details/44305689

转载于:https://www.cnblogs.com/tenglong2500/p/4793992.html

你可能感兴趣的文章
微信小程序开发初体验
查看>>
dos批处理(bat)运行exe
查看>>
关键字
查看>>
Pycharm安装Markdown插件
查看>>
上传图片并预览
查看>>
哈夫曼编码_静态库
查看>>
【转】redo与undo
查看>>
C#更新程序设计
查看>>
常用Request对象获取请求信息
查看>>
解决升级系统导致的 curl: (48) An unknown option was passed in to libcurl
查看>>
Shell命令-内置命令及其它之watch、date
查看>>
Java Session 介绍;
查看>>
spoj TBATTLE 质因数分解+二分
查看>>
Django 模型层
查看>>
第8章-方法
查看>>
dedecms讲解-arc.listview.class.php分析,列表页展示
查看>>
Microsoft SQL Server Transact-SQL
查看>>
Font: a C++ class
查看>>
Extjs6 经典版 combo下拉框数据的使用及动态传参
查看>>
Java四种引用包括强引用,软引用,弱引用,虚引用
查看>>