Posted By

kutyadog on 10/26/12


Tagged

Blocks


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

gurin2014


xcode Objective C - block examples


 / Published in: Objective C
 

Examples of how Objective-c objects work

  1. //-----demonstrate how blocks hold their captured state
  2. NSDate *date = [NSDate date];
  3. void (^now)(void) = ^ {
  4. NSLog(@"The date and time is %@", date);
  5. };
  6. now();
  7. sleep(5);
  8. date = [NSDate date];
  9. now();
  10.  
  11.  
  12. //--------------
  13. int (^triple)(int) = ^(int number) {
  14. return number * 3;
  15. };
  16. int result1 = triple(2);
  17.  
  18. int (^multiply)(int, int) = ^(int x, int y) {
  19. return x * y;
  20. };
  21. int result2 = multiply(2, 3);
  22. NSLog(@"result %i", result2);
  23. result2 = multiply(5, 3);
  24. NSLog(@"result %i", result2);

Report this snippet  

You need to login to post a comment.