4

There are a number of discussions about enumerating through CMSensorDataList around, however, they all have the same example:

extension CMSensorDataList: SequenceType {
    public func generate() -> NSFastGenerator {
        return NSFastGenerator(self)
    }
}

Which doesn't work on ios11 for multiple reasons (NSFastGenerator doesn't exist, SequenceType has been renamed to Sequence).

How do I enumerate through a CMSensorDataList in modern swift?

Andrew Spott
  • 3,457
  • 8
  • 33
  • 59

1 Answers1

0

if swift 4 and 5 it will work

extension CMSensorDataList: Sequence {
    public typealias Iterator = NSFastEnumerationIterator
    public func makeIterator() -> NSFastEnumerationIterator {
        return NSFastEnumerationIterator(self)
    }
}

now you will need to do like blow

let rec = CMSensorRecorder() 
if let list = rec.accelerometerData(from: date1, to: date2) {
    for item in list {
        if let data = item as? CMRecordedAccelerometerData {
            let x = data.acceleration.x
            print("X: \(x)")
        }
    }
}
Sultan Ali
  • 2,497
  • 28
  • 25