创建块变量

时间:2012-11-01 11:47:34

标签: iphone objective-c ios nsarray objective-c-blocks

我有函数,我将块变量 blockIfLoadingImageFromServer 添加到数组中:

- (UIImage *)getImageWithblockIfLoadingImageFromServer:(void (^)(UIImage *imageCompleted))blockIfLoadingImageFromServer
{
      [array addObject:blockIfLoadingImageFromServer];
}

然后我想得到这个变量,但我不知道如何。我试过了:

(void (^)(UIImage *imageCompleted))blockIfLoadingImageFromServer = [array objectAtIndex:0];
void (^)(UIImage *imageCompleted) *blockIfLoadingImageFromServer = [array objectAtIndex:0];

但是这会产生错误(语法错误)。我怎么能这样做?

1 个答案:

答案 0 :(得分:3)

我通常typedef让我的生活更轻松:

typedef void (^ImageBlock)(UIImage*);

然后:

- (void) doSomethingWithBlock: (ImageBlock) block
{
    [array addObject:[block copy]];
}

- (void) doSomethingElse
{
    ImageBlock block = [array lastObject];
    …
}