【问题标题】:'withUnsafeBytes' is deprecated: use `withUnsafeBytes<R>(_: (UnsafeRawBufferPointer)'withUnsafeBytes' 已弃用:使用 `withUnsafeBytes<R>(_: (UnsafeRawBufferPointer)
【发布时间】:2021-06-01 00:51:28
【问题描述】:

我收到 Xcode 警告:

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

代码是:

_document = data.withUnsafeBytes({ (p: UnsafePointer<Int8>) -> htmlDocPtr? in
                return htmlReadMemory(p, Int32(data.count), nil, nil, 0)
            }) 

 

我是一个更客观的人,我绊倒了 1 小时来关闭这个警告但没有运气有什么想法吗?

我已经检查了这个答案: Swift 5.0: 'withUnsafeBytes' is deprecated: use `withUnsafeBytes<R>(...)

编辑: 还检查了这个并得到了相同的警告:
https://github.com/leonbreedt/FavIcon/issues/23#issue-365477776

【问题讨论】:

  • 这不是复制的(用这个改变你的代码):github.com/leonbreedt/FavIcon/blob/…
  • 宾果游戏。非常感谢 !你能在正确的地方回答,所以我会标记它已解决?
  • 我已经检查了这个答案。答案中建议了baseAddress.assumingMemoryBound 解决方案(我在您的第一个已删除的问题中将其标记为重复)。

标签: swift


【解决方案1】:

根据doc中的内容

你可以用这个来改变

_document = data.withUnsafeBytes { ptr in
    htmlReadMemory(ptr.baseAddress?.assumingMemoryBound(to: Int8.self), Int32(data.count), nil, nil, 0)
}

【讨论】:

  • 我认为返回值比产生赋值副作用更好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多