AFNetworking缺少setCompletionBlock ios swift

时间:2015-02-18 13:53:03

标签: ios swift afnetworking-2 progress downloading

我尝试从url下载带有进度的pdf文件。一切都好。但我找不到setCompletionBlock,为什么? 这是我的工作代码:

println("progress: \(0.0)")

    let request: NSURLRequest = NSURLRequest(URL: NSURL(string: document.link)!)
    let operation: AFURLConnectionOperation = AFHTTPRequestOperation(request: request)
    let paths: NSArray = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
    let filePath: NSString = paths.objectAtIndex(0).stringByAppendingPathComponent("pdf_\(document.id).pdf")
    operation.outputStream = NSOutputStream(toFileAtPath: filePath, append: false)

    operation.setDownloadProgressBlock({(bytesRead, totalBytesRead, totalBytesExpectedToRead) -> Void in

        var total: CGFloat = CGFloat(totalBytesRead) / CGFloat(totalBytesExpectedToRead)
        println("progress: \(total)")


    })

    //operation.setCompletionBlock ... can't found this block?!

    operation.start()

1 个答案:

答案 0 :(得分:3)

您正在将AFHTTPRequestOperation投射到AFURLConnectionOperation

let operation: AFURLConnectionOperation = AFHTTPRequestOperation(request: request)

setCompletionBlockWithSuccess中定义的是AFHTTPRequestOperation,而不是AFURLConnectionOperation

相反,只需让operation成为AFHTTPRequestOperation

let operation = AFHTTPRequestOperation(request: request)

然后它会成功识别setCompletionBlockWithSuccess