Drew O'Meara
Drew O'Meara

Reputation: 470

Is there a way to declare an objective-C block typedef whose arguments contain that typedef?

Here's an interesting one for the objective-C gurus out there...

Is there a way to declare an objective-C block typedef that contains an argument of that typedef?

typedef BOOL (^SSCellAction) ( UITableViewController* inTVC, SSCellAction inChainedAction );

The idea is that I wanted to used chained menu action system that allows a chain of work/response to occur (usually 1-3 items). When the last action invokes, it passes nil for inChainedAction. Since this seems relatively trivial to imagine, I'll be dammed if I can't figure out how to declare it without llvm saying no. :)

Upvotes: 4

Views: 437

Answers (1)

newacct
newacct

Reputation: 122429

rmaddy's comment is correct. Just as in C, a typedef cannot use itself. Basically, a typedef does not make a real type, but just makes an alias that the compiler expands out at compile-time. It is always possible to manually expand all typedefs in your program yourself (which is sometimes an instructive exercise), so that your program is written without typedefs. However, a recursive typedef cannot be expanded.

Some possible workarounds:

  • Use id as the parameter type, and cast back into the right type inside the block. This loses type safety.
  • Or, use a struct type of one member, the block. A struct is a real type, so it can be used within its definition. The downside of this is that you explicitly "wrap" the block into the struct type to pass it, and explicitly "unwrap" the struct into the block by accessing the field when you need to call it. This way is type-safe.

Upvotes: 2

Related Questions