如何修复循环遍历数组中索引的代码?

时间:2019-04-11 01:48:32

标签: arrays swift bluetooth-lowenergy

我正在尝试修复此代码。目前,我收到此错误。

  

下标'subscript(_ :)'要求'UInt8.Bound'符合'FixedWidthInteger'

  

下标'subscript(_ :)'要求'UInt8'符合'RangeExpression'

不确定在此情况下错误是什么意思。

let v = characteristic.value;

let count = (v?.count)! / MemoryLayout<UInt8>.size
var array = [UInt8](repeating: 0, count: count)
v?.copyBytes(to: &array, count:count * MemoryLayout<UInt8>.size)

let numCharacteristics = (v![0] & 0xf0) >> 4
let numDataPoints = v![0] & 0x0f

for index in 0..<numDataPoints-1 {

  datapoint = v![index] + (v![index+1] << 8)
  datapointArray.append(datapoint!)

}

1 个答案:

答案 0 :(得分:0)

Data类型不能用UInt8下标。试试:

for index in 0..<numDataPoints-1 {
      let intIndex = Int(index)
      let datapoint = v![intIndex] + (v![intIndex+1] << 8)
      datapointArray.append(datapoint)
}