Search code examples
swiftcore-motionwatchos-2

Swift watchOS 2 - CMSensorDataList


Short: I don't know how to extract the CMRecordedAccelerometerData from the CMSensorDataList after getting one from the CMSensorRecorder. Apple isn't providing any documentation, yet.

Maybe someone has a hint for me? ;)

func startMovementDetection(){
    var accDataList = self.cmSensorRecorder!.accelerometerDataFrom(self.startDate, to: NSDate()) as CMSensorDataList

    CMRecordedAccelerometerData() //that's the class i want to extract from CMSensorDataList
}

Okay, problem solved with this one here: NSFastEnumeration in Swift

With Swift 3.0 it changes to:

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

Solution

  • //First make the extension tu use enumerate in the for-in loop
    extension CMSensorDataList: SequenceType {
        public func generate() -> NSFastGenerator {
            return NSFastGenerator(self)
        }
    }
    
    //Now you can query the recorded data
    func printData(){
        let date = NSDate()
        let recorder = CMSensorRecorder()
        let sensorData: CMSensorDataList = recorder.accelerometerDataFromDate(initialDate!, toDate: date)!
    
        for (index, data) in sensorData.enumerate() {
            print(index, data)
        }
    }