"Programming with Objective-c" eighth chapter working with Blocks

Source: Internet
Author: User

Blocks is objective-c objects, which means they can be added to collections like Nsarray or nsdictionary.

Block syntax--no parameter version definition (value of block)
^{    NSLog (@ "This isa block");}

Statement
void (^simpleblock) (void);

similar to int i;

Assign value
Simpleblock = ^{    NSLog (@ "This isa block");    }

Similar to i = 2;

When declaring a definition
void (^simpleblock) (void) = ^{    NSLog (@ "This isa block");}

similar to int i = 2;

Call
Simpleblock ();

Block syntax--with parameter version definition
^ (doubledouble  secondvalue) {    return firstvalue * secondvalue;}

Or

^Double (doubledouble  secondvalue) {    return firstvalue *  SecondValue;}

Statement
Double (^multiplytwovalues) (doubledouble);

Assign value
Multiplytwovalues = ^ (doubledouble  secondvalue) {    return Firstvalue * secondvalue;};

When declaring a definition
Double (^multiplytwovalues) (doubleDouble) =^ ( double double secondvalue) {    return Firstvalue * secondvalue;};

Call
double result = Multiplytwovalues (2,3);

__block modifier
int  the ; void (^testblock) (void) = ^{    // This is just the block definition and does not perform the function inside    NSLog (@ "Integer is:%i "  testblock ();     // Block Call output

Value is captured when the block is defined.

When the block is defined, the value is copied to itself, so the value is not affected by the outside world.

int  the ; void (^testblock) (void) = ^{    // This is just the block definition and does not perform the function inside    NSLog (@ "Integer is:%i " , Aninteger);      - ; Testblock ();     // Block Call output

Because Aninteger is declared as a __block variable, it storage is GKFX with the block declaration.

At this point the value inside the block shares the same memory as the value outside

Block and Self's Revenge

It's important to take care when capturing self because it's easy-to-create a strong reference cycle when capturing self.

Because the variables are __strong decorated by default (see here), always be aware of the reference to self within the block (as long as the Auto keyword is quoted, as the block will automatically capture)

If self is defined as a block, then self has a strong pointer to the block (for example, the block is a strong member variable of self); if self is used inside the block, The block also defaults to copying a strong pointer to self, which forms strong reference cycle.

Workaround: Create a __weak type pointer to self in front of the block and use the pointer inside the block.

Example

typeof (self) weakself = self;    // Learn to do this, OH typeof (self)Self.simpleblock = ^{    [weakself f];}; ... self.simpleblock ();

But what if there is another block inside the block? It is best to strongly reference weakself, at which time strongself strongly refers to weakself instead of self, so strong reference cycle is not formed

typeof (self) weakself = self;    // Learn to do this writing Oh typeof (self)Self.simpleblock = ^{    [weakself f]    ; typeof (weakself) strongself = weakself;     = ^{        [strongself f];    };    Self.simpleblock2 ();}; ... self.simpleblock ();

Graphic

A function is best to have only one block parameter, preferably in the last

A Block should always being the last Argument to a Method.

It's best practice to use only one block argument to a method.

Use typedef to define a block
int (^sum) (intint= ^ (intint  b) {    return a +b;}

Or

void (^xyzsimpleblock) (void); @property (copy) Xyzsimpleblock Blockproperty;

Use Copy to modify the property of a block
void (^blockproperty) (void);

Non-arc must be written copy,because a block needs to being copied to keep track of it captured state outside of the original scope
It doesn't matter if you write without copy under ARC, so it's a best practice to set copy property for block whether it's ARC or not.

"Programming with Objective-c" eighth chapter working with Blocks

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.