IOS block programming guide 3 concepts Overview

Source: Internet
Author: User

IOS block programming guide 3 concepts Overview
Conceptual Overview (Overview)

Block objects provide a way for you to create an ad hoc function body as an expression in C, and C-derived extensions ages such as Objective-C and C ++. in other versions ages 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.

The block object provides you with the possibility of creating C and C extension languages, such as ad hoc functions in objective-C and C ++. In other language Environments, block objects are sometimes referred to as "closure". Here, block objects are generally called in the vernacular: "block" unless there is a complicated Standard C block code in the scope.


Block Functionality (block Function)

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.

    A block is an anonymous Inline code set:

     

    Like a function, there is a list of type parameters. There is a type of inferred or declared return value that can capture the status within the lexical scope it defines. You can modify the status in the lexical scope at will and share the modification with the block in the same scope. It can be in the lexical scope (stack) after being destroyed, continue to share and modify the status in the lexical scope. You can copy a block or even extend it to other threads for execution (or add a loop queue to your process ). The compiler and runtime arrange that the variables related to all blocks are stored in the copy lifecycle of all blocks. Although block can be used in pure C and C ++, block objects are often used in objective-C.

     

    Usage (used)

    Blocks represent typically small, self-contained pieces of code. as such, they're participating ularly 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.

      A block is usually expressed as a small independent code block. Therefore, a block is often used as an encapsulation unit that may be executed simultaneously, an item in a set, or a callback at the end of an operation. Blocks is an effective replacement for callback functions for the following two reasons: they allow you to write code after the call point (the function content is over. Therefore, block is often used as a parameter of the framework function. They allow you to access local variables. Compared to all context information required by the callback function, block only needs to directly access local variables.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.