Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
809 views
in Technique[技术] by (71.8m points)

core motion - 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)
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
//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)
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...