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

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

问题描述

方法,它可以下载附件并接受阻止作为返回下载进度的参数:

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 /em>调用回调块.

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. 将委托转换为基于块的方法.
  1. implementing what I want through delegation
  2. converting the delegation into a block based approach.

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

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天全站免登陆