使用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来处理下载进度。