gpt4 book ai didi

swift - 如何从字节数组 ([UInt8]) 中获取一个字节 (UInt8)?

转载 作者:行者123 更新时间:2023-11-28 16:11:27 25 4
gpt4 key购买 nike

func readByte(bytes: [UInt8], offset: UInt8) -> UInt8 {
return bytes[offset] // Error: Cannot subscript a value of type '[UInt8]' with an index of type 'UInt8'
}

如果将偏移量更改为任何其他 Int 将导致相同的错误。但是,如果我使用 bytes[0] 就没有问题。可能是因为 Swift 知道期望的类型并相应地转换 0 。我想知道那是什么类型。

最佳答案

数组是由 Int 索引的集合:

public struct Array<Element> : RandomAccessCollection, MutableCollection {
// ...
public typealias Index = Int
// ...
public subscript(index: Int) -> Element
// ...
}

在你的情况下:

func readByte(bytes: [UInt8], offset: Int) -> UInt8 {
return bytes[offset]
}

关于swift - 如何从字节数组 ([UInt8]) 中获取一个字节 (UInt8)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39506499/

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