AFHTTPRequestOperation

AFHTTPRequestOperation

我试图从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()

最佳答案

您正在将AFHTTPRequestOperation转换为AFURLConnectionOperation:

let operation: AFURLConnectionOperation = AFHTTPRequestOperation(request: request)

但是setCompletionBlockWithSuccess是在AFHTTPRequestOperation中定义的,而不是在AFURLConnectionOperation中定义的。

而是让operationAFHTTPRequestOperation:
let operation = AFHTTPRequestOperation(request: request)

然后它将成功识别setCompletionBlockWithSuccess

10-08 05:29