'withUnsafeBytes' is deprecated

Hey there,

I've been kicking my head with this one. Just hoping for some help:

func hexString(data: Data) -> String {
            return data.withUnsafeBytes { (bytes: UnsafePointer<UInt8>) -> String in
                let buffer = UnsafeBufferPointer(start: bytes, count: data.count)
                return buffer.map{String(format: "%02hhx", $0)}.reduce("", {$0 + $1})

the code above throws the following warning:

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

Change bytes: UnsafePointer<UInt8> to bytes: UnsafeRawBufferPointer, and then adjust the body of the closure accordingly. It should end up being less code in the end I think.

As @David_Smith said, the withUnsafeBytes() method passing a typed pointer to the closure has been deprecated in favor of a method which passes a raw pointer to the closure, see also

You can also let the compiler infer the closure parameters automatically (and omit the return keyword for single-expression closures/functions):

func hexString(data: Data) -> String {
    data.withUnsafeBytes {
        $0.map{String(format: "%02hhx", $0)}.reduce("", { $0 + $1 })

Thanks, martin, that worked like a charm!