gpt4 book ai didi

swift5 - 'withUnsafeMutableBytes' 已弃用 : use `withUnsafeMutableBytes

转载 作者:行者123 更新时间:2023-12-05 00:58:48 25 4
gpt4 key购买 nike

我对 Xcode 非常熟悉(提前道歉)。试图使一些旧代码栩栩如生。尝试迁移到 Swift 5 时获得以下信息。

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

目标:我需要做的就是适本地修改代码并完成。

我查看了其他 Stack Overflow 消息,搜索了各种文章,尝试了不同的东西,但无法快速确定需要更改的内容。我相信对于了解更多的人来说,解决方案非常简单。

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

_ = responseData.withUnsafeMutableBytes
{
mfError = MFMediaIDResponse_GetAsString(mfMediaIdResponsePtr.pointee, MFString($0), responseDataLength)
}

最佳答案

这是我的示例,当刷新 withUnsafeMutableBytes 的旧代码时

希望对你有帮助

旧的:

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

新的:

_ = 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)
}
}

解释:

使用 UnsafeMutablePointer<ContentType> ,你会在它的闭包中得到一个 unsafeMutablePointer。

要访问它的内存,所以需要输入 bindMemory ,

有关 Apple Pointer Doc 的更多详细信息

关于swift5 - 'withUnsafeMutableBytes' 已弃用 : use `withUnsafeMutableBytes<R>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56417261/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com