UnsafePointer 不再适用于 swift 3 [英] UnsafePointer no longer works in swift 3
问题描述
在我从 swift 2 转换为 swift 3 后,下面提到的行会弹出一个错误
let value = UnsafePointer(array1).pointee
<块引用>
'init' 不可用:使用 'withMemoryRebound(to:capacity:_)' 暂时将内存视为另一种布局兼容类型.
在swift2中就像
let value = UnsafePointer(array1).memory
谁能解释一下?抱歉,我对 swift3 还很陌生
在我对
进行更改后let abc = UnsafePointer(array1).withMemoryRebound(to: <#T##T.Type#>, capacity: <#T##Int#>, <#T##body: (UnsafeMutablePointer) 抛出 -> Result##(UnsafeMutablePointer) 抛出 -> Result#>)
但仍然应该给变量输入什么值?抱歉,我四处搜索,但太糟糕了,我找不到解决方案
你可以试试这个:
let rawPointer = UnsafeRawPointer(array1)让指针 = rawPointer.assumingMemoryBound(to: UInt32.self)让值 = 指针.pointee
原始指针是用于访问非类型数据的指针.
assumingMemoryBound(to:)
可以从 UnsafeRawPointer
转换为 UnsafePointer
.
After I convert from swift 2 to swift 3, there is an error pop up for the below metioned line
let value = UnsafePointer<UInt32>(array1).pointee
'init' is unavailable: use 'withMemoryRebound(to:capacity:_)' to temporarily view memory as another layout-compatible type.
in swift2 it is like
let value = UnsafePointer<UInt32>(array1).memory
Can someone explain please? Sorry I'm quite new to swift3
After i have make the changes to
let abc = UnsafePointer<UInt32>(array1).withMemoryRebound(to: <#T##T.Type#>, capacity: <#T##Int#>, <#T##body: (UnsafeMutablePointer<T>) throws -> Result##(UnsafeMutablePointer<T>) throws -> Result#>)
but still what value should go in to the variable? Sorry, i have search around but too bad i can't find a solution
You can try this:
let rawPointer = UnsafeRawPointer(array1)
let pointer = rawPointer.assumingMemoryBound(to: UInt32.self)
let value = pointer.pointee
Raw pointer is a pointer for accessing untype data.
assumingMemoryBound(to:)
can convert from an UnsafeRawPointer
to UnsafePointer<T>
.
Reference :Swift 3.0 Unsafe World
这篇关于UnsafePointer 不再适用于 swift 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!