IOS Block Programming Guide 3 Concept overview

Source: Internet
Author: User

Conceptual Overview (Overview)

Block objects provide a-to-create ad hoc function body as an expression in C, and c-derived languages such As objective-c and C + +. In the other languages and environments, a block object is sometimes also called a "closure". Here, they was typically referred to colloquially as "blocks", unless there was scope for confusion with the standard C ter M for a block of code.

The Block object gives you the possibility of creating the C and C language extensions, such as ad hoc functions in Objective-c and C + +. In other locales, block objects are sometimes referred to as "closure", where they are commonly referred to as "blocks", unless there is a complex standard C block code in scope.


Block functionality (block function)

A block is an anonymous inline collection of code:

  • Have a typed argument list just like a function

  • Have 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 and 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 STA CK 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 is preserved for the life of all copies of The block. Although blocks is 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 type argument list.
    • There is an inferred or declared return value type
    • You can capture the state
    • You can modify the status
    • Modify the
    • You can continue to share and modify the state in the lexical scope after the lexical scope (stack) is destroyed.
you can copy a block or even postpone it to another thread to execute (or join a loop queue in your own process). The compiler and runtime schedule all the relevant variables of the block to be kept in the lifetime of the copy of all blocks. Block objects are often used in objective-c, although blocks can be used in pure C and C + +.

Usage (usage)

Blocks represent typically small, self-contained pieces of code. As such, they ' re particularly useful as a means of encapsulating units of work, the May is executed concurrently, or over Items in a collection, or as a callback when another operation have finished.

blocks is a useful alternative to traditional callback functions for the main reasons:

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

      They Allow access to local variables.

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

blocks are usually represented as small, independent chunks of code. As a result, blocks are often used as a package unit that can be executed concurrently, or as an item in a collection, or as a callback at the end of an operation. blocks is a valid substitute for the callback function for the following two reasons:
    1. They allow you to write code after the call point (the function content is already over). So block often appears as a parameter to the frame function.
    2. They allow you to access local variables. Instead of all the contextual information required by the callback function, the block simply accesses the local variable directly.
This article original, reproduced please indicate the source: http://blog.csdn.net/zhenggaoxing/article/details/44305689
Previous

IOS Block Programming Guide 3 Concept overview

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.