gpt4 book ai didi

ios - UnsafePointer 不再适用于 swift 3

转载 作者:搜寻专家 更新时间:2023-10-30 22:30:40 25 4
gpt4 key购买 nike

在我从 swift 2 转换为 swift 3 后,下面提到的行弹出错误

let value = UnsafePointer<UInt32>(array1).pointee

'init' is unavailable: use 'withMemoryRebound(to:capacity:_)' to temporarily view memory as another layout-compatible type.

在swift2中就像

let value = UnsafePointer<UInt32>(array1).memory

有人可以解释一下吗?对不起,我对 swift3 很陌生

修改后

let abc = UnsafePointer<UInt32>(array1).withMemoryRebound(to: <#T##T.Type#>, capacity: <#T##Int#>, <#T##body: (UnsafeMutablePointer<T>) throws -> Result##(UnsafeMutablePointer<T>) throws -> Result#>)

但仍然应该向变量中输入什么值?抱歉,我四处搜索,但太糟糕了,我找不到解决方案

最佳答案

你可以试试这个:

let rawPointer = UnsafeRawPointer(array1)
let pointer = rawPointer.assumingMemoryBound(to: UInt32.self)
let value = pointer.pointee

原始指针是用于访问非类型数据的指针。

assumingMemoryBound(to:)可以从 UnsafeRawPointer 转换至 UnsafePointer<T> .

引用:Swift 3.0 Unsafe World

关于ios - UnsafePointer 不再适用于 swift 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39824340/

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