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