Sending 'NSProgress *__strong *' to parameter of incompatible type 'void (^ _Nullable)(NSProgress *

使用AFNetWorking 3.0下载文件,网络上给出了很多示例代码,在XCode 7.2上运行,直接报错。


原始代码:

NSURLSessionDownloadTask *downTask = [manager downloadTaskWithRequest:request progress:&progressTemp  destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {

        //告诉服务器下载的文本保存的位置在那里

        NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];

        NSLog(@"file = %@",targetPath);

       return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];

    } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {

        NSLog(@"response = %@,filePath = %@",response,filePath);

        NSFileManager *fileManager = [NSFileManager defaultManager];

       if ([fileManager fileExistsAtPath:[resourcePath path] isDirectory:NULL]) {

            [fileManager removeItemAtURL:resourcePath error:NULL];

        }

       BOOL seccess =[fileManager moveItemAtURL:filePath toURL:resourcePath error:NULL];

       if (seccess)

        {}

    }];



报错信息:

Sending 'NSProgress *__strong *' to parameter of incompatible type'void (^ _Nullable)(NSProgress * _Nonnull __strong)'



字面意思是传递的参数类型不符,

查看AFNetWorking源文件,找到对应的SDK,

- (NSURLSessionDownloadTask *)downloadTaskWithRequest:(NSURLRequest *)request

                                             progress:(void (^)(NSProgress *downloadProgress)) downloadProgressBlock

                                          destination:(NSURL * (^)(NSURL *targetPath,NSURLResponse *response))destination

                                    completionHandler:(void (^)(NSURLResponse *response,NSURL *filePath, NSError *error))completionHandler



确定progress后面的参数是一个入参为NSProgress的block,而我们传递进去的是一个指针,类型不匹配,所以报错。简单修改后的代码如下:

NSURLSessionDownloadTask *downTask = [manager downloadTaskWithRequest:request progress:^(NSProgress *downloadProgress){NSLog(@"progress is %@", downloadProgress);}  destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {}


应该专门定义一个block来处理下载进度。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值