如何在 Swift 3 中将 UInt16 转换为 UInt8?

2023-01-03移动开发问题
60

本文介绍了如何在 Swift 3 中将 UInt16 转换为 UInt8?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我想将 UInt16 转换为 UInt8 数组,但收到以下错误消息:

I want to convert UInt16 to UInt8 array, but am getting the following error message:

'init' 不可用:使用 'withMemoryRebound(to:capacity:_)'暂时将内存视为另一种布局兼容的类型.

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

代码:

    let statusByte: UInt8 = UInt8(status)
    let lenghtByte: UInt16 = UInt16(passwordBytes.count)

    var bigEndian = lenghtByte.bigEndian

    let bytePtr = withUnsafePointer(to: &bigEndian) {
        UnsafeBufferPointer<UInt8>(start: UnsafePointer($0), count: MemoryLayout.size(ofValue: bigEndian))
    }

推荐答案

如错误信息所示,你必须使用 withMemoryRebound()将指向 UInt16 的指针重新解释为指向 UInt8 的指针:

As the error message indicates, you have to use withMemoryRebound() to reinterpret the pointer to UInt16 as a pointer to UInt8:

let bytes = withUnsafePointer(to: &bigEndian) {
    $0.withMemoryRebound(to: UInt8.self, capacity: MemoryLayout.size(ofValue: bigEndian)) {
        Array(UnsafeBufferPointer(start: $0, count: MemoryLayout.size(ofValue: bigEndian)))
    }
}

闭包是用指针($0)调用的,这些指针只有效在闭包的整个生命周期内,不得传递给外部供以后使用.这就是创建 Array 并将其用作返回值的原因.

The closures are invoked with pointers ($0) which are only valid for the lifetime of the closure and must not be passed to the outside for later use. That's why an Array is created and used as return value.

但是有一个更简单的解决方案:

There is a simpler solution however:

let bytes = withUnsafeBytes(of: &bigEndian) { Array($0) }

解释: withUnsafeBytes 调用带有 UnsafeRawBufferPointer 的闭包到 bigEndian 变量的存储.由于 UnsafeRawBufferPointerUInt8Sequence,因此是一个数组可以使用 Array($0) 来创建.

Explanation: withUnsafeBytes invokes the closure with a UnsafeRawBufferPointer to the storage of the bigEndian variable. Since UnsafeRawBufferPointer is a Sequence of UInt8, an array can be created from that with Array($0).

这篇关于如何在 Swift 3 中将 UInt16 转换为 UInt8?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

硬件音量按钮更改应用程序音量
Hardware Volume buttons change in app volume(硬件音量按钮更改应用程序音量)...
2024-08-12 移动开发问题
10

恢复游戏 cocos2d
Resume game cocos2d(恢复游戏 cocos2d)...
2024-08-12 移动开发问题
6

[ios.cocos2d+box2d]如何禁用自动旋转?
[ios.cocos2d+box2d]how to disable auto-rotation?([ios.cocos2d+box2d]如何禁用自动旋转?)...
2024-08-12 移动开发问题
7

cocos2D iPhone中的UItextfield文本对齐问题
UItextfield text alignment issue in cocos2D iPhone(cocos2D iPhone中的UItextfield文本对齐问题)...
2024-08-12 移动开发问题
7

从 Documents 目录存储和读取文件 iOS 5
Storing and reading files from Documents directory iOS 5(从 Documents 目录存储和读取文件 iOS 5)...
2024-08-12 移动开发问题
9

如何在 cocos2d 环境之外实现 cocos2d 游戏的虚拟摇杆?
How can I implement a virtual joystick for a cocos2d game outside the cocos2d environment?(如何在 cocos2d 环境之外实现 cocos2d 游戏的虚拟摇杆?)...
2024-08-12 移动开发问题
13