在我从 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.
参考:Swift 3.0 不安全的世界
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).memoryCan 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.pointeeRaw 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
发布评论