IOS block編程指南 3 概念總覽

來源:互聯網
上載者:User

IOS block編程指南 3 概念總覽
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 因為如下兩個原因成為回呼函數的一個有效替代者: 他們允許你在調用點(函數內容已經結束啦)之後寫代碼。因此呢block常常作為架構函數的參數出現。他們允許你訪問本地變數。相比於回呼函數要求操作的所有上下文資訊,block只需要直接存取本地變數就行了。  

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.