Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'withUnsafeMutableBytes' is deprecated: use `withUnsafeMutableBytes<R>

Tags:

swift5

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)
        }
like image 247
user11590570 Avatar asked Nov 16 '25 18:11

user11590570


1 Answers

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

like image 87
black_pearl Avatar answered Nov 18 '25 21:11

black_pearl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!