当前位置: 首页 > 面试题库 >

Swift watchOS 2-CMSensorDataList

水昊阳
2023-03-14
问题内容

简短:我不知道从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)
    }
}

问题答案:
//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)
    }
}


 类似资料:

相关阅读

相关文章

相关问答