一尘不染

Swift watchOS 2-CMSensorDataList

swift

简短:我不知道从CMSensorRecorder中获取一个后,如何从CMSensorDataList中提取CMRecordedAccelerometerData。苹果尚未提供任何文档。

也许有人对我有暗示?;)

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

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

好的,这里解决了这个问题:Swift中的NSFastEnumeration

借助Swift 3.0,它变为:

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

阅读 284

收藏
2020-07-07

共1个答案

一尘不染

//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)
    }
}
2020-07-07