Block Preliminary understanding

Source: Internet
Author: User

Recently learned about the application of block (in fact, it should be mastered), and then a little tidying up a bit.

A block is a C-level syntax and a runtime feature similar to a function (function pointer) in standard C, but its operation requires compiler and runtime support, and blocks are well supported from the beginning of iOS4.0.

The benefits of block, mainly 1. Used for callbacks particularly convenient, 2. You can extend the object's area of action. (The __block keyword, which transforms a local variable into a global variable). But the block is allocated on the stack by default, which is the stack above, so its function area is the current one.

The scope of the object can be extended this block is not said, the main word for callbacks. The block is used to callback this because the proxy can also implement its same functionality. Therefore, the block and the delegate agent made a comparison, found that, more than similar, almost like a TMD. Here I cite the example of "Dog and human Affairs", that is, the watchdog found the enemy to report to people.

First Look at block:

Definition of two classes: Dog.h Person.h

Dog.h

@interface Dog: nsobject

{

int _id; ID number

Nstimer *timer; Start a timer

int Barkcount; The number of times a dog barks, as well as the number of reports to the owner

// define a blocks variable

void (^barkcallback) (Dog *thisdog,int count);

}

@property (assign)int ID;


for person .

outward exposing a function Setbark

-(void) Setbark: (void (^) (Dog *thisdog,int count)) Eachbark;


@end


DOG.M:

#import "Dog.h"

@implementation Dog

Override the Init method, turn on a timer, and report the situation to the owner every 1s

-(ID) init

{

self = [superinit];

if (self) {

Timer = [nstimerscheduledtimerwithtimeinterval:1.0f target:self selector:@selector( Updatetimer:)userInfo:nilrepeats:YES];

}

returnself;

}


-(void) Updatetimer: (ID) arg

{

Barkcount+ +;

NSLog(@ "Dog%d bark count%d",_id,barkcount );

// report to Person/xiaoli ( for communication )

// call the blocks in Person/xiaoli

if (barkcallback) {

barkcallback(self,barkcount);

    }    

}


-(void) Setbark: (void (^) (Dog *thisdog,int count)) Eachbark

{

Receive the block from person.

barkcallback = Eachbark;

}


Person.h:

#import <Foundation/Foundation.h>

#import "Dog.h"


@interface Person: NSObject

@property (nonatomic,retain)Dog *dog;

@end



PERSON.M:

#import "Person.h"


@implementation Person

-(void) Setdog: (dog *) dog

{

[Dog Setbark: ^ (dog *dog,int barkcount) {

NSLog (@ "person, dog%d bark%d", dog.) ID, Barkcount);

}];

}

@end


Inside the controller call:

dog *dog = [[Dogalloc] init];

Dog. ID = ten;

person *person = [[personalloc] init];

Person. Dog = dog;

Run the program and look at the print:

2014-12-17 19:28:48.727 blockdemo-2[2752:199621] Dog Bark 1

2014-12-17 19:28:48.728 blockdemo-2[2752:199621] person, Dog bark 1

2014-12-17 19:28:49.728 blockdemo-2[2752:199621] Dog Bark 2

2014-12-17 19:28:49.728 blockdemo-2[2752:199621] person, Dog bark 2

2014-12-17 19:28:50.727 blockdemo-2[2752:199621] Dog Bark 3

2014-12-17 19:28:50.727 blockdemo-2[2752:199621] person, Dog bark 3

......


Here's a look at the agent:

Same: Dog.h:

#import <Foundation/Foundation.h>


@class Dog;


@protocol barkcallback <nsobject>


-(void) Barkcallbacktoperson: (dog *) Dog andcount: (int) Barkcount;


@end


@interface Dog: nsobject

{

int _id;

Nstimer *timer;

int Barkcount;

}


@property (assign) int ID;


@property (assign) ID <barkcallback> delegate;


@end


DOG.M:

#import "Dog.h"


@implementation Dog

-(instancetype) init

{

self = [super init];

if (self) {

Timer = [nstimerscheduledtimerwithtimeinterval:1.0 target: Selfselector: @selector ( Updatetimer:)userInfo:nilrepeats:YES];

}


return self;

}


-(void) Updatetimer: (ID) arg

{

Barkcount+ +;

NSLog(@ "Dog%d bark count%d",_id,barkcount) ;

if ([_delegate respondstoselector:@selector(barkcallbacktoperson:andcount:)]) {

[_delegatebarkcallbacktoperson:self andcount: Barkcount ];

}

}


@end


Person.h:

#import <Foundation/Foundation.h>

#import "Dog.h"


@interface Person: NSObject

@property (nonatomic,retain)Dog *dog;

@end


PERSON.M:

#import "Person.h"


@implementation Person


-(void) Setdog: (dog *) dog

{

Dog. delegate = (ID)self;

}


-(void) Barkcallbacktoperson: (dog *) Dog andcount: (int) Barkcount

{

NSLog(@ "person dog%d bark%d", [dogID], barkcount);

}

@end


In the controller:

person *xiaoli = [[personalloc] init];

dog *dog = [[Dogalloc] init];

[Dog SetID:ten];

[Xiaoli setdog:d og];

[[nsrunloopcurrentrunloop] run]; At this point, you need to pay attention to add this code, or the program will collapse. No, you can, but you have to change somewhere else. Specific why, did not understand, haha ~ ~ Look master enlighten.

Run for a look. Print results:

2014-12-17 19:53:35.092 delegatedemo-dog[3366:213877] Dog bark Count 1

2014-12-17 19:53:35.093 delegatedemo-dog[3366:213877] person Dog Bark 1

2014-12-17 19:53:36.091 delegatedemo-dog[3366:213877] Dog Bark Count 2

2014-12-17 19:53:36.091 delegatedemo-dog[3366:213877] person Dog Bark 2

2014-12-17 19:53:37.092 delegatedemo-dog[3366:213877] Dog Bark Count 3

2014-12-17 19:53:37.092 delegatedemo-dog[3366:213877] person Dog Bark 3

......

Same


Of course, you can also in the block program just inside or delegate program inside to remove Person.h, directly in the controller to perform callback or Proxy method also line, let the dog report the situation. How to do it is OK.












Block Preliminary understanding

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.