Pete Pete - 26 days ago 5
Swift Question

URLSession Conversion to Swift 3

I have converted to Swift 3 and receiving the error message


'Cannot convert value of type (UnsafeRawPointer, NSRange,
UnsafeMutablePointer)->() to (UnsafeBufferPointer,
Data.index, inout Bool)-> Void.


My code:

func urlSession(_ session: URLSession, dataTask: URLSessionDataTask,
didReceive data: Data) {
data.enumerateBytes{[weak self]
(pointer: UnsafeRawPointer,
range: NSRange,
stop: UnsafeMutablePointer<ObjCBool>) in
let newData = Data(bytes: UnsafePointer<UInt8>(pointer), count: range.length)
self!.mutableData.append(newData)
} }


What do I need to adapt to make it work?

Answer

You are already getting Data object and you want to append the data, then why are you doing all these things

func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data){
    responseData?.append(data)
}

This will work fine.

For reference, look at https://github.com/ankitthakur/SwiftNetwork/tree/master/Sources/Shared