The application of the block of iOS learning notes in iOS development

Source: Internet
Author: User

first, what is blocksBlock is a C-level syntax and a feature of the runtime, similar to a function (function pointer) in standard C, but its operation requires compiler and runtime support, and is well-supported from ios4.0.
second, in iOS development, under what circumstances use blockIn addition to being able to define parameter lists, return types, blocks can also obtain state (such as local variables) within the lexical scope that is defined, and can modify these states under certain conditions, such as using the __block variable. In addition, these modifiable states are shared among multiple blocks within the same lexical scope, and can continue to share or modify these states, even if the lexical scope (such as stack expansion, out of scope) is out. In general, blocks are encapsulated in short snippets of code that are used as work units, often for concurrent tasks, traversal, and callbacks.
third, how to declare the block (in contrast to the C language function declaration) Four, C function and blocks callInt (*cfunc) (int a) function calls int result = Cfunc (10); Int (^bfunc) (int a) function calls int result = Bfunc (10);
Five, __block key wordsThe inside of a block can refer to its own extraterritorial variables, including static variables, extern variables or free variables (when defining a variable, if no modifier is stored, by default the free variable Auto,auto variable is saved in the stack. In addition to auto, there are storage modifiers such as register,static, which are read-only in the block for free variables. In addition to the introduction of block, a special __block keyword variable storage modifier is introduced.
Vi. Several small examples of block

Java code
  1. #Import <Cocoa/Cocoa.h>
  2. int main (int argc, char *argv[])
  3. {
  4. @autoreleasepool {
  5. NSLog (@"Hello World");
  6. void (^myblocks) (void) = NULL;
  7. Myblocks = ^ (void) {
  8. NSLog (@"in blocks");
  9. };
  10. NSLog (@"before Myblocks");
  11. Myblocks ();
  12. NSLog (@"after Myblocks");
  13. Int (^MYBLOCKS2) (int A, int b) = ^ (int A, int b) {
  14. int c = a + B;
  15. return C;
  16. };
  17. NSLog (@"before Blocks2");
  18. int ret = MYBLOCKS2 (ten, 20);
  19. NSLog (@"after BLOCKS2 ret%d", ret);
  20. //Here if you do not add __block will be an error
  21. __blockint sum = 0;
  22. Int (^MYBLOCKS3) (int A, int b) = ^ (int A, int b) {
  23. sum = a + b;
  24. RETURN3;
  25. };
  26. MYBLOCKS3 ( 30);
  27. NSLog (@"sum is%d", sum);
  28. }
  29. Returnnsapplicationmain (argc, (Constchar * *) argv);
  30. }
 #import <cocoa/cocoa.h>int Main (int argc, char *argv[]) {@autoreleasepool {        NSLog (@ "Hello world");        void (^myblocks) (void) = NULL;        Myblocks = ^ (void) {NSLog (@ "in blocks");        };        NSLog (@ "before Myblocks");        Myblocks ();                        NSLog (@ "after Myblocks");            Int (^MYBLOCKS2) (int a, int b) = ^ (int a, int b) {int c = a + B;        return C;        };        NSLog (@ "before Blocks2");        int ret = MYBLOCKS2 (10, 20);                NSLog (@ "After BLOCKS2 ret%d", ret);        If you do not add __block here will be an error __blockint sum = 0;            Int (^MYBLOCKS3) (int a, int b) = ^ (int a, int b) {sum = a + b;        RETURN3;        };        MYBLOCKS3 (20, 30);    NSLog (@ "sum is%d", sum); } returnnsapplicationmain (argc, (Constchar * *) argv);} 

Printing results are as follows 2012-09-03 10:23:20.878 blocktest[407:403] Hello world 2012-09-03 10:23:20.880 blocktest[407:403] before myblocks 2012-09-03 10:23:20.881 blocktest[407:403] in blocks 2012-09-03 10:23:20.881 blocktest[407:403] after myblocks 2012-09-03 10:23:20.882 blocktest[407:403] before Blocks2 2012-09-03 10:23:20.882 blocktest[407:403] after BLOCKS2 ret 30 2012-09-03 10:23:20.882 blocktest[407:403] sum is 50
Vii. Block Write callback example 1, Dog.h

Java code
  1. #Import <Foundation/Foundation.h>
  2. @interface Dog:nsobject {
  3. int _id;
  4. Nstimer *timer;
  5. int barkcount;
  6. //define a blocks variable
  7. void (^barkcallback) (Dog *thisdog, int count);
  8. }
  9. @property (assign) int ID;
  10. Exposing an interface outward
  11. -(void) Setbark: (  void (^) (Dog *thisdog, int count)) Eachbark;
  12. @end
#import <Foundation/Foundation.h> @interface dog:nsobject {    int _id;    Nstimer *timer;    int barkcount;        Defines a blocks variable    void (^barkcallback) (Dog *thisdog, int count);} @property (assign) int id;//exposes an interface-(void) Setbark: (void (^) (Dog *thisdog, int count)) Eachbark; @end

2, DOG.M

Java code
  1. #Import "Dog.h"
  2. @implementation Dog
  3. @synthesize ID = _id;
  4. -(ID) init
  5. {
  6. Self = [Superinit];
  7. if (self) {
  8. //every 1s call updatetimer method
  9. Timer = [Nstimerscheduledtimerwithtimeinterval:1.0ftarget:selfselector:@selector (updatetimer:) UserInfo:  Nilrepeats:yes];
  10. }
  11. returnself;
  12. }
  13. -(void) Updatetimer: (ID) arg
  14. {
  15. Barkcount + +;
  16. NSLog (@"dog%d bark count%d", _id, Barkcount);
  17. //reporting to person objects
  18. if (barkcallback) {
  19. //Call blocks sent from person
  20. Barkcallback (self, barkcount);
  21. }
  22. }
  23. -(void) Setbark: (void (^) (Dog *, int)) Eachbark
  24. {
  25. [Barkcallbackrelease];
  26. Barkcallback = [Eachbark copy];
  27. }
  28. -(void) Dealloc
  29. {
  30. [Barkcallbackrelease];
  31. [Superdealloc];
  32. }
  33. @end
#import "Dog.h" @implementation dog@synthesize id = _id;-(ID) init{Self    = [Superinit];    if (self) {        //every 1s call once updatetimer method        timer = [Nstimerscheduledtimerwithtimeinterval:1.0ftarget:selfselector : @selector (Updatetimer:) UserInfo:nilrepeats:YES];            }    Returnself;} -(void) Updatetimer: (ID) arg{    barkcount + +;    NSLog (@ "Dog%d bark count%d", _id, Barkcount);    Report to the Person object    if (barkcallback) {        //Call blocks        barkcallback (self, barkcount) passed from person;}    } -(void) Setbark: (void (^) (DOG *, int)) eachbark{    [barkcallbackrelease];    Barkcallback = [Eachbark copy];} -(void) dealloc{    [barkcallbackrelease];    [Superdealloc];} @end

3, Person.h

Java code
    1. #Import <Foundation/Foundation.h>
    2. #Import "Dog.h"
    3. @interface Person:nsobject
    4. {
    5. Dog *_dog;
    6. }
    7. @property (retain) Dog *dog;
    8. @end
#import <Foundation/Foundation.h> #import "Dog.h" @interface person:nsobject{    Dog *_dog;} @property (retain) Dog *dog; @end

4, PERSON.M

Java code
  1. #Import "Person.h"
  2. @implementation person
  3. @synthesize dog = _dog;
  4. -(void) Setdog: (dog *) Dog
  5. {
  6. if (_dog! = dog) {
  7. [_dogrelease];
  8. _dog = [dog retain];
  9. [_dogsetbark:^ (Dog *thisdog, int count) {
  10. NSLog (@"person dog%d count%d", [Thisdog ID], count);
  11. }];
  12. }
  13. }
  14. -(dog *) dog
  15. {
  16. Return_dog;
  17. }
  18. -(void) Dealloc
  19. {
  20. Self.dog = nil;
  21. [Superdealloc];
  22. }
  23. @end
#import "Person.h" @implementation person@synthesize dog = _dog;-(void) Setdog: (dog *) dog{    if (_dog! = dog) {        [_do Grelease];        _dog = [dog retain];                [_dogsetbark:^ (Dog *thisdog, int count) {            NSLog (@ "Person dog%d count%d", [Thisdog ID], count);        }];}    } -(DOG *) dog{    Return_dog;} -(void) dealloc{    self.dog = nil;    [Superdealloc];} @end

5, MAIN.M

Java code
  1. #Import <Foundation/Foundation.h>
  2. #Import "Person.h"
  3. #Import "Dog.h"
  4. int main (int argc, Constchar * argv[])
  5. {
  6. @autoreleasepool {
  7. //Insert code here ...
  8. NSLog (@"Hello, world!");
  9. Person *person = [[Personalloc] init];
  10. Dog *dog = [[Dogalloc] init];
  11. [Dog SetID:10];
  12. [Person Setdog:dog];
  13. [Dog release];
  14. While (1) {
  15. [[Nsrunloopcurrentrunloop] run];
  16. }
  17. [Person release];
  18. }
  19. return 0;
  20. }
 #import <Foundation/Foundation.h> #import "Person.h" #import "Dog.h" int main (int        ARGC, Constchar * argv[]) {@autoreleasepool {//Insert code here ...        NSLog (@ "Hello, world!");        Person *person = [[Personalloc] init];        Dog *dog = [[Dogalloc] init];        [Dog Setid:10];        [Person Setdog:dog];        [Dog release];        while (1) {[[Nsrunloopcurrentrunloop] run];            } [person release]; } return 0;} 

6. Printing results 2012-09-03 11:21:08.551 blockdelegate[549:403] Hello, world! 2012-09-03 11:21:09.554 blockdelegate[549:403] Dog bark count 1 2012-09-03 11:21:09.555 blockdelegate[549:403] Person D OG count 1 2012-09-03 11:21:10.554 blockdelegate[549:403] Dog bark count 2 2012-09-03 11:21:10.555 blockdelegate[549 : 403] Person dog count 2 2012-09-03 11:21:11.553 blockdelegate[549:403] Dog bark count 3 2012-09-03 11:21:11.554 Blo CKDELEGATE[549:403] Person Dog ten count 3 2012-09-03 11:21:12.554 blockdelegate[549:403] Dog bark count 4 2012-09-03 11 : 21:12.555 blockdelegate[549:403] Person dog count 4 2012-09-03 11:21:13.553 blockdelegate[549:403] Dog bark Count 5 2012-09-03 11:21:13.553 blockdelegate[549:403] Person dog count 5 2012-09-03 11:21:14.553 blockdelegate[549:403] Dog 1 0 Bark Count 6 2012-09-03 11:21:14.554 blockdelegate[549:403] Person Dog Count 6

The application of the block of iOS learning notes in iOS development

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.