Home | History | Annotate | Download | only in blocks
      1 #import "ivars-in-blocks.h"
      2 
      3 typedef int (^my_block_ptr_type) (int);
      4 
      5 @interface IAmBlocky()
      6 {
      7   int _hidden_ivar;
      8   my_block_ptr_type _block_ptr;
      9 }
     10 
     11 @end
     12 
     13 @implementation IAmBlocky
     14 
     15 + (int) addend
     16 {
     17   return 3;
     18 }
     19  
     20 + (void) classMethod
     21 {
     22   int (^my_block)(int) = ^(int foo)
     23   {
     24     int ret = foo + [self addend];
     25     return ret; // Break here inside the class method block.
     26   };
     27   printf("%d\n", my_block(2));
     28 }
     29 
     30 - (void) makeBlockPtr;
     31 {
     32   _block_ptr = ^(int inval)
     33   {
     34     _hidden_ivar += inval;
     35     return blocky_ivar * inval; // Break here inside the block.
     36   };
     37 }
     38 
     39 - (IAmBlocky *) init
     40 {
     41   blocky_ivar = 10;
     42   _hidden_ivar = 20;
     43   // Interesting...  Apparently you can't make a block in your init method.  This crashes...
     44   // [self makeBlockPtr];
     45   return self;
     46 }
     47 
     48 - (int) callABlock: (int) block_value
     49 {
     50   if (_block_ptr == NULL)
     51     [self makeBlockPtr];
     52   int ret = _block_ptr (block_value);
     53   [IAmBlocky classMethod];
     54   return ret;
     55 }
     56 @end
     57 
     58