3

I am very green with Xcode (apologize in advance). Trying to bring some old code to life. Getting the following with trying to move to Swift 5.

withUnsafeMutableBytes' is deprecated: use withUnsafeMutableBytes<R>(_: (UnsafeMutableRawBufferPointer) throws -> R) rethrows -> R instead

Goal: All I need to do is modify the code appropriately and be done.

I have looked at other Stack Overflow messages, searched various articles, experimented with different things, but can't quickly determine what needs to change. I am sure the solution is super simple for someone that knows more.

var responseData = Data(count: Int(responseDataLength))

        _ = responseData.withUnsafeMutableBytes
        {
            mfError = MFMediaIDResponse_GetAsString(mfMediaIdResponsePtr.pointee, MFString($0), responseDataLength)
        }
shim
  • 9,289
  • 12
  • 69
  • 108
user11590570
  • 31
  • 1
  • 3
  • Working on simple app with AM library. For reference: func extractServerResponseFromMediaIdentificationResponse(mfMediaIdResponsePtr: UnsafeMutablePointer, error: NSErrorPointer) -> AudioMagicIdServerResponse? { ... var responseData = Data(count: Int(responseDataLength)) _ = responseData.withUnsafeMutableBytes { mfError = MFMediaIDResponse_GetAsString(mfMediaIdResponsePtr.pointee, MFString($0), responseDataLength) } ... } – user11590570 Jun 02 '19 at 17:18
  • Possible duplicate of [Errors when update code to avoid deprecation warnings withUnsafeMutableBytes in swift 5](https://stackoverflow.com/questions/55384250/errors-when-update-code-to-avoid-deprecation-warnings-withunsafemutablebytes-in) – rmaddy Jun 02 '19 at 18:03
  • Hi, welcome to SO! I would like to help, but I don't understand the problem either. But maybe [this post](https://forums.developer.apple.com/thread/115227) can help you! – Reinhard Männer Jun 02 '19 at 21:16
  • This could help https://forums.swift.org/t/withunsafebytes-data-api-confusion/22142 – Eva Madrazo Jun 03 '19 at 21:09
  • I believe I looked those links (stack overflow, swift, and apple) a few times. I will work to review again and see what I need to do. Given my experience, maybe I just don't get it. I didn't get how those example mapped for me. – user11590570 Jun 05 '19 at 03:12

1 Answers1

10

Here is my example, when refreshing old code of withUnsafeMutableBytes

hope it helps

The old one:

_ = data.withUnsafeMutableBytes { (bytes: UnsafeMutablePointer<UInt8>) in
        memcpy((ioData.pointee.mBuffers.mData?.assumingMemoryBound(to: UInt8.self))!, bytes, dataCount)
    }

The new one:

_ = data.withUnsafeMutableBytes { (rawMutableBufferPointer) in
        let bufferPointer = rawMutableBufferPointer.bindMemory(to: UInt8.self)
        if let address = bufferPointer.baseAddress{
            memcpy((ioData.pointee.mBuffers.mData?.assumingMemoryBound(to: UInt8.self))!, address, dataCount)
        }
    }

Explains:

use UnsafeMutablePointer<ContentType>, you get an unsafeMutablePointer in its closure.

To access to its memory, so need to typed it with bindMemory,

more details on Apple Pointer Doc

Tim Sylvester
  • 22,897
  • 2
  • 80
  • 94
black_pearl
  • 2,549
  • 1
  • 23
  • 36