如何向块添加额外的参数 [英] how to add an extra argument to a block

查看:29
本文介绍了如何向块添加额外的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

方法,可以下载附件并接受一个块作为返回下载进度的参数:

Mailcore has a cool method that downloads an attachment and accepts a block as a parameter to return download progress:

- (CTCoreAttachment *)fetchFullAttachmentWithProgress:(CTProgressBlock)block;

其中 CTProgressBlock 定义为

where CTProgressBlock is defined as

typedef void (^CTProgressBlock)(size_t curr, size_t max);

所以通常我会像这样使用它:

so typically I would use it like so:

//AttachmentDownloader.m
int fileNum = x; // explained later
CTCoreAttachment* fullAttachment = 
    [attachment fetchFullAttachmentWithProgress:^(size_t curr, size_t max) {
    NSLog(@"::: this is curr: %zu", curr);
    NSLog(@"::: this is max: %zu\n\n", max);
}];

问题是最后一个方法由我的主 UI 类 FileBucket.m 调用,而这个类依次为许多不同的 UI 元素获取许多附件.我希望这个回调方法向 FileBucket.m 报告这个进度属于哪个附件......所以换句话说我想要一些类似的东西:

the problem is that this last method is called by my main UI class FileBucket.m and this class is in turn fetching many attachments for many different UI elements in sequence. I would like this callback method to report back to FileBucket.m which attachment this progress belongs to.. so in other words i want something along the lines of:

// FileBucket.m
[AttachmentDownloader runMultiple:attachmentTree 
                     withProgress:^(size_t curr, size_t max, int fileNum) {
    NSLog(@"::: this is curr: %zu", curr);
    NSLog(@"::: this is max: %zu\n\n", max);
    NSLog(@"::: this progress belongs to element %d", fileNum);
}];

我知道这很难解释/说明......还有一件事情:AttachmentDownloader.m 知道这个进度是关于哪个附件......但它只是想把它传回 FileBucket.m 每次回调块被调用.

I know this is hard to explain/illustrate.. one extra thing: AttachmentDownloader.m is aware of which attachment this progress is about.. but it just wants to pass it back to FileBucket.m every time the callback block is called.

推荐答案

我想出来的方法是把它分成两步:

the way I figured this out is by breaking it into two steps:

  1. 通过委托
  2. 实现我想要的
  3. 将委托转换为基于块的方法.

由于这个问题很难用英语解释,我认为最好用示例代码来解释:)

and since this problem is kind of hard to explain by english, I figured it's best explained by sample code :)

委托解决方案:

// AttachmentDownloader.m
+(void)runMultiple:(NSDictionary *)attachmentTree 
      withDelegate:(id<AttachmentDownloaderDelegate>)delegate  {
    ..

    CTCoreAttachment* fullAttachment = 
       [attachment fetchFullAttachmentWithProgress:^(size_t curr, size_t max) {
            NSLog(@"::: this is curr: %zu", curr);
            NSLog(@"::: this is max: %zu\n\n", max);
            NSLog(@"::: this is attachment uid %@", [message uid]);
            [delegate deliverProgressWithInfo:@{@"uid":[message uid],
                                                @"curr":[NSNumber numberWithInt:curr],
                                                @"max":[NSNumber numberWithInt:max]}];
     }];


// FileBucket.m
[AttachmentDownloader runMultiple:attachmentTree withProgress:^(size_t curr, size_t max) {
    NSLog(@"::: this is curr: %zu", curr);
    NSLog(@"::: this is max: %zu\n\n", max);
} withDelegate:self];

// delegate method
-(void)deliverProgressWithInfo:(NSDictionary *)dict {
    NSLog(@"filebucket: this is info being updated %@", dict);
}

阻止解决方案:

// AttachmentDownloader.m
+(void)runMultiple:(NSDictionary *)attachmentTree 
      withProgress:(void(^)(NSDictionary *))block {
    ..

    CTCoreAttachment* fullAttachment = 
        [attachment fetchFullAttachmentWithProgress:^(size_t curr, size_t max) {
        NSLog(@"::: this is curr: %zu", curr);
        NSLog(@"::: this is max: %zu\n\n", max);
        NSLog(@"::: this is attachment uid %@", [message uid]);
        block(@{@"uid":  [message uid],
                @"curr": [NSNumber numberWithInt:curr],
                @"max":  [NSNumber numberWithInt:max]});
    }];


// FileBucket.m
-(void)downloadSelectedAttachments {
    NSDictionary* attachmentTree = [self getAttachmentTree];
    [AttachmentDownloader runMultiple:attachmentTree withProgress:^(NSDictionary * dict) {
        NSLog(@"filebucket: this is info being updated %@", dict);
    }];
}

这篇关于如何向块添加额外的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆