Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 4th, 2012  |  syntax: None  |  size: 1.17 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Objective C send message to creator
  2. @implementation SomeInterface
  3.  
  4. -(void)DoSomething
  5. {
  6.      MyObj * mo = [MyObj new];
  7.      [mo doJob];
  8. }
  9. end
  10.        
  11. // create a typedef for our type of completion handler, to make
  12. // syntax cleaner elsewhere
  13. typedef void (^MyObjDoJobCompletionHandler)(void);
  14.  
  15. @interface MyObj
  16.  
  17. - (void)doJobWithCompletionHandler:(MyObjDoJobCompletionHandler)completionHandler;
  18.  
  19. @end
  20.        
  21. - (void)doJobWithCompletionHandler:(MyObjDoJobCompletionHandler)completionHandler
  22. {
  23.     /* do job here ... */
  24.  
  25.     // we're done, so give the completion handler a shout.
  26.     // We call it exactly like a C function:
  27.     completionHandler();
  28.  
  29.     /* alternatives would have been to let GCD dispatch it,
  30.     but that would lead to discussion of GCD and a bunch of
  31.     thread safety issues that aren't really relevant */
  32. }
  33.        
  34. -(void)DoSomething
  35. {
  36.      MyObj * mo = [MyObj new];
  37.      [mo doJobWithCompletionHandler:
  38.          ^() // you can omit the brackets if there are no parameters, but
  39.              // that's a special case and I don't want to complicate things...
  40.          {
  41.              NSLog(@"whoop whoop, job was done");
  42.  
  43.              // do a bunch more stuff here
  44.          }
  45.      ];
  46. }