返回指向块的指针

时间:2011-12-07 19:58:35

标签: objective-c objective-c-blocks

我在类中有一个静态块变量。如何声明属性或实例选择器以将该块返回或发送给调用者?

这是我的静态阻止:

static NSResultComparison(^myBlock)(id obj1, id obj2);

1 个答案:

答案 0 :(得分:5)

最好typedef你的阻止。

//interface.h
typedef NSResultComparison (^ComparisonBlock)(id obj1, id obj2);

@interface ...
...

-(ComparisonBlock) getComparisonBlock;

@end

//implementation.m
//Here is your static block implementation
static ComparisonBlock myStaticBlock = ^(id obj1, id obj2)
{
    ...
    return result;
};

@implementation ...

-(ComparisonBlock)getComparisonBlock
{
    return myStaticBlock;
}

@end