久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何在 Swift 3 中將 UInt16 轉(zhuǎn)換為 UInt8?

How to convert UInt16 to UInt8 in Swift 3?(如何在 Swift 3 中將 UInt16 轉(zhuǎn)換為 UInt8?)
本文介紹了如何在 Swift 3 中將 UInt16 轉(zhuǎn)換為 UInt8?的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我想將 UInt16 轉(zhuǎn)換為 UInt8 數(shù)組,但收到以下錯(cuò)誤消息:

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

'init' 不可用:使用 'withMemoryRebound(to:capacity:_)'暫時(shí)將內(nèi)存視為另一種布局兼容的類型.

'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))
    }

推薦答案

如錯(cuò)誤信息所示,你必須使用 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)調(diào)用的,這些指針只有效在閉包的整個(gè)生命周期內(nèi),不得傳遞給外部供以后使用.這就是創(chuàng)建 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.

但是有一個(gè)更簡單的解決方案:

There is a simpler solution however:

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

解釋: withUnsafeBytes 調(diào)用帶有 UnsafeRawBufferPointer 的閉包到 bigEndian 變量的存儲.由于 UnsafeRawBufferPointerUInt8Sequence,因此是一個(gè)數(shù)組可以使用 Array($0) 來創(chuàng)建.

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).

這篇關(guān)于如何在 Swift 3 中將 UInt16 轉(zhuǎn)換為 UInt8?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

how to set scrollview content size in swift 3.0(如何在 swift 3.0 中設(shè)置滾動視圖內(nèi)容大小)
Create partial-screen UIPageViewController programmatically(以編程方式創(chuàng)建部分屏幕 UIPageViewController)
UIImageView zoom and pinch in UIScrollView(UIImageView 在 UIScrollView 中縮放和捏合)
Make background color change during scroll(在滾動期間更改背景顏色)
How to set UIScrollView Height in Swift(如何在 Swift 中設(shè)置 UIScrollView 高度)
Disable UIPageViewController bouncing - Swift(禁用 UIPageViewController 彈跳 - Swift)
主站蜘蛛池模板: 欧美日韩在线视频一区 | xxxxx黄色片 欧美一区免费 | 五月婷婷激情网 | 亚洲精品久久久久久久久久久 | 成人免费福利 | 91嫩草精品| 欧美一卡二卡在线 | 天天拍天天操 | 久久tv在线观看 | 亚洲三级国产 | 亚洲国产精品一区在线观看 | 久色激情 | 国产日韩电影 | 一级毛毛片 | 免费的日批视频 | av中文天堂| 欧美成人第一页 | 蜜月va乱码一区二区三区 | 国产欧美日韩综合精品一 | 日韩在线| 国产精品www| 免费国产一区二区视频 | 日韩毛片在线视频 | 99久久电影 | 99reav| 欧美成人一区二区三区 | 久久精品中文 | 一区二区不卡视频 | 中文字幕第二十页 | 日韩av在线一区二区三区 | 日韩av免费在线观看 | 男女网站免费观看 | 日本淫视频 | 黄网站色大毛片 | 麻豆91av | 亚洲精品18 | 91xx在线观看| 亚洲精品久久久久久久久久久 | 欧美精品一区二区三区在线 | av黄色在线 | 一区二区电影 |