Block and Proxy in iOS

Source: Internet
Author: User

The introduction of block from iOS4, is code block, C language structure class

Basic structure return value (^block name) (parameter): Int (^blockname) (int): return value is int, parameter is an int value of block called Blockname

When we use a block:

// Code Examples int (^blockexample) (int= ^ (int n) {return n2;}; int result = Blockexample (1);

Personal use scenarios:

One: Animation

The first time I touched block is the animation block, before the UIView animation block is written between begin and end of the need to deal with the animation effect, the end is to be implemented in the delegate, and a page all the animation end callback in a method to distinguish, indeed a bit of egg pain

After that there is a lot of introduction to block mode:

[UIView animatewithduration:1 delay:0 Options:uiviewanimationoptionbeginfromcurrentstate animations:^{        // animate content     } completion:^(BOOL finished) {         // callbacks after the animation is complete     }];

Second: Data request callback

The previous request callback will be completed in the proxy method, and now add a complete block directly to the originating request method

Data request code block

[Self postrequest:request getreustl:^{//get result}];

Three: Enumeration callback

Before we would use a for loop to access each element in the array, we would now use the BLOCK:

An example of an array enum

[Array enumerateobjectsusingblock:^ (ID obj, Nsuinteger idx, BOOL *stop) {     //process each element in array }];

Four: Multithreading GCD

Back to block in multi-threaded: more intuitive

0), ^{           / /                          operation required in multithreading ^{                               // main thread operation             });        });

* Modify variables in block

We can't change the value of a variable unless you precede the variable with __block.

the wrong approach int 0 ; [Self excuteblock:^{      2int0; Self Excuteblock:^{      2;}];

* We sometimes encounter blocks that retain the entire page, making it impossible to call Dealloc when it returns, thus unable to free memory

So we're going to use weak reference retain cycle in the block.

Like what:

typeof (self) __weak weakself = self;[ Self Excutetask:task success:^(id responsemodel) {        //handle success    weakSelf.bottomView.isLike = YES;    [Weakself dosomething];
} failure:^ (Nserror *error) { //handle failure}];

* When to use a proxy, when to use block

Public interface, more methods also choose to use Delegate to decouple
iOS has many examples, such as the most commonly used tableviewdelegate,textviewdelegate

Asynchronous and simple callbacks are better with block
iOS has many examples, such as the Common network library Afnetwork,asihttp Library, the Uialertview class.

Block and Proxy in iOS

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.