6

I was hoping someone could help me understand the syntax of blocks when used as members of a class. I have some code that's actually working just fine:

@class Structure;
typedef void (^StructureDeleteCallback)(Structure *);

@interface StructureListDelegate : NRFCTableDelegate
{
    StructureDeleteCallback _structureDeleteCallback;
}

@property (nonatomic, copy) StructureDeleteCallback structureDeleteCallback;

@end

This works, but I would like to understand the syntax of the typedef statement; and whether or not it's actually required to use typedef.

What I've read says that using typedef in this situation is recommended because it makes the code a lot clearer; but I've been unable to get it to compile at all when trying to do this without typedef. My understanding of typedef was that the syntax was basically:

typedef [actual type] [new name for type];

Such as:

typedef double CLLocationDegrees;

But the syntax of my typedef statement doesn't match this. So my questions are:

  • How can the syntax of my typedef statement be so different from other typedef statements / what does the syntax I'm using actually mean to the compiler?
  • Is it possible to have a block as a member of a class without using typedef?
GendoIkari
  • 11,734
  • 6
  • 62
  • 104

1 Answers1

7

I myself have asked a question along the lines of yours here: Block references as instance vars in Objective-C

See my answers here and here.

Community
  • 1
  • 1
Jacob Relkin
  • 161,348
  • 33
  • 346
  • 320
  • Ah! I wasn't able to get it to work without using typedef because I didn't realize the syntax for defining the iVar would be completely different. I thought I'd still have to have [typename] [iVarName]; – GendoIkari Dec 01 '10 at 15:51
  • So just to clarify one last thing... whether it's in the typedef, or just declaring a block, is it basically just that blocks have a different syntax for declaring them than other types do? The compiler just sees the ^ symbol and knows that the name of the variable is after the ^, as opposed to after the name of the type like it is with int x;? Thanks! – GendoIkari Dec 01 '10 at 15:57
  • Had to step away for a bit. Thanks again! – GendoIkari Dec 01 '10 at 18:21