gpt4 book ai didi

ios - 从 Objective C 转换为 Swift 后,问题位移位 UInt8

转载 作者:搜寻专家 更新时间:2023-11-01 06:38:59 25 4
gpt4 key购买 nike

我正在使用 NSURLConnection 将视频流式传输到我的应用程序中的 UIImage。我的部分代码在 Objective C 中有效,但我在转换为 Swift 时遇到了问题:

func connection(connection: NSURLConnection, didReceiveData data: NSData) {

//...other code

UInt32 sizeOfJPEG_1; //same type as was in Objective C

var payloadsize = [UInt8](count: 4, repeatedValue: 0x00) //was uint8_t in Objective C
data.getBytes(&payloadsize[1], range: NSMakeRange(12, 3))
payloadsize[0] = 0
sizeOfJPEG_1 = (payloadsize[1] << 16) + (payloadsize[2] << 8) + payloadsize[3]//here is the problem
//UInt32(sizeOfJPEG_1 = (payloadsize[1] << 16) + (payloadsize[2] << 8) + payloadsize[3]) //the way I am currently dealing with converting my shifting and additions to the correct sizeOfJPEG_1 UInt32 argument type
//..more code
}

我这里有两个问题:

  1. 我不确定将 payloadsize UInt8 移位和添加转换为参数 UInt32 的最佳方法 enter image description here
  2. 更重要的是,可能首先要弄清楚的是我移动 UInt8 << 16 时出现的运行时错误,在 Objective-C 中类型是 uint8_t,我不知道这在 Objective-C 中是否是合法的类型操作uint8_t 只是不在 Swift 中,类型为 UInt8

我收到溢出错误,因为我左移了 UInt8 << 16 以及 UInt8 << 8: fatal error :移位量大于类型大小(以位为单位)

enter image description here

我想我明白 Objective C 会悄悄地移动 uint8_t << 16 而不会崩溃,但我不知道如何计算这个,uint8_t << 16 的结果是什么,不会是 0 吗? (uint8_t 定义为无符号字符)

最佳答案

与 (Objective-)C 不同,Swift 不提倡更小的整数类型到 intunsigned int,所以你必须明确地这样做(之前移动数据):

let sizeOfJPEG_1 = UInt32(payloadsize[1]) << 16 + UInt32(payloadsize[2]) << 8 + UInt32(payloadsize[3])

关于ios - 从 Objective C 转换为 Swift 后,问题位移位 UInt8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37910441/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com