xcode Objective C - block examples


/ Published in: Objective C
Save to your folder(s)

Examples of how Objective-c objects work


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.