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,将导致相同的错误。但是,如果使用字节[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]
}