gellezzz
gellezzz

Reputation: 1253

AFNetworking missing setCompletionBlock ios swift

I trying to download a pdf file from url with progress. All it's ok. But i can't found setCompletionBlock, why? This is my working code:

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()

Upvotes: 2

Views: 475

Answers (1)

Rob
Rob

Reputation: 437432

You are casting your AFHTTPRequestOperation to a AFURLConnectionOperation:

let operation: AFURLConnectionOperation = AFHTTPRequestOperation(request: request)

But setCompletionBlockWithSuccess is defined in AFHTTPRequestOperation, not AFURLConnectionOperation.

Instead, just let operation be a AFHTTPRequestOperation:

let operation = AFHTTPRequestOperation(request: request)

Then it will recognize setCompletionBlockWithSuccess successfully.

Upvotes: 3

Related Questions