'withUnsafeBytes' is deprecated: use `withUnsafeBytes<R>(_: (UnsafeRawBufferPointer) throws -> R) rethrows -> R` instead

   let bytesWritten = data.withUnsafeBytes{outputStream?.write($0, maxLength: data.count)}
        print("bytes written :\(bytesWritten!) to \(String(describing: fileURL))")

how fix it? thanks

1 Like

As the error message suggests, what you want is a closure that uses an UnsafeRawBufferPointer instead of the deprecated version that uses a typed pointer. E.g., something like this:

let bytesWritten = data.withUnsafeBytes {
    let byteBuffer = $0.bindMemory(to: UInt8.self)
    return outputStream?.write(byteBuffer.baseAddress!, maxLength: byteBuffer.count)

thank you, warning is fixed :+1:

Hah, I hit this for the first time just tonight, as well. The compiler warning threw me for a loop, too, because it’s really not clear that it’s actually referring to a different method.

I wonder if the compiler could provide a fix-it? Granted, there are multiple possible ways to select the non-deprecated overload, but I suspect the solution mentioned in this thread – which is the same one I happened to go with – is what most people want.