Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
    }
}
like image 992
Narsail Avatar asked Jun 28 '15 15:06

Narsail


3 Answers

//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)
    }
}
like image 123
Marcus Vinicius Kuquert Avatar answered Oct 24 '22 15:10

Marcus Vinicius Kuquert


Here's a Swift 4 approach. First, you’ll need to make CMSensorDataList conform to Sequence by means of an extension:

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

Now you can iterate over CMSensorDataList to obtain CMRecordedAccelerometerData instances, each consisting of a timestamp and an acceleration:

let rec = CMSensorRecorder() // and d1 and d2 are Dates
if let list = rec.accelerometerData(from: d1, to: d2) {
    for datum in list {
        if let accdatum = datum as? CMRecordedAccelerometerData {
            let accel = accdatum.acceleration
            let t = accdatum.timestamp
            // do something with data here
        }
    }
}
like image 39
matt Avatar answered Oct 24 '22 13:10

matt


Marcus's answer in Swift 4:

//First make the extension to use enumerate in the for-in loop
extension CMSensorDataList: Sequence {
    public typealias Iterator = NSFastEnumerationIterator

    public func makeIterator() -> NSFastEnumerationIterator {
        return NSFastEnumerationIterator(self)
    }
}

//Now you can query the recorded data
func printData(){
    let date = Date()
    let recorder = CMSensorRecorder()
      let accelerometerData = recorder.accelerometerData(from: startDate, to: endDate)

     for (index, data) in (accelerometerData?.enumerated())! {
                    print(index, data)
      }
}
like image 22
GarySabo Avatar answered Oct 24 '22 14:10

GarySabo