Is it possible to define a block as a member of a class?

I am trying to implement a very simple strategic class in Objective-C, which allows you to define strategies within a string, rather than defining through inheritance. Currently my code is as follows:

@interface SSTaskStrategy : NSObject { (NSArray *)(^strategy)(void); } @end 

I thought this would work, but I get an error

Expected list-qualifier-qualifier before '(' token

Any ideas how to make this work?

+7
objective-c objective-c-blocks
source share
2 answers

You should put parentheses around NSArray * in your ivar definition:

 @interface SSTaskStrategy : NSObject { NSArray * (^strategy)(void); } @end 

In addition, I highly recommend that you use typedef for clarity:

 typedef NSArray * (^Strategy)(void); @interface SSTaskStrategy : NSObject { Strategy block; } @end 

This allows you to reference this block with the name Strategy instead of using the funky syntax every time you want to reference it.

+17
source share
 @interface SSTaskStrategy : NSObject { NSArray* (^strategy)(void); } 

You do not need to put ( ) around the return type.

+2
source share

All Articles