UnsafePointer 不再适用于 swift 3

编程入门 行业动态 更新时间:2024-10-16 00:20:56
本文介绍了UnsafePointer 不再适用于 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.

参考: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).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

本文发布于:2023-11-26 11:19:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1633789.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:适用于   UnsafePointer   swift

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!