Swift watchOS 2 - CMSensorDataList
Swift watchOS 2 - CMSensorDataList
简而言之:我不知道如何在从 CMSensorRecorder 获取 CMSensorDataList 后从 CMSensorDataList 中提取 CMSensorRecorder。
Apple 尚未提供任何文档。
也许有人给我提示? ;)
func startMovementDetection(){
var accDataList = self.cmSensorRecorder!.accelerometerDataFrom(self.startDate, to: NSDate()) as CMSensorDataList
CMRecordedAccelerometerData() //that's the class i want to extract from CMSensorDataList
}
好的,问题在这里解决了:NSFastEnumeration in Swift
在 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)
}
}
Marcus 在 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)
}
}
这是 Swift 4 的方法。首先,您需要通过扩展使 CMSensorDataList 符合 Sequence:
extension CMSensorDataList: Sequence {
public typealias Iterator = NSFastEnumerationIterator
public func makeIterator() -> NSFastEnumerationIterator {
return NSFastEnumerationIterator(self)
}
}
现在您可以迭代 CMSensorDataList 以获得 CMRecordedAccelerometerData 实例,每个实例由一个 timestamp
和一个 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
}
}
}
简而言之:我不知道如何在从 CMSensorRecorder 获取 CMSensorDataList 后从 CMSensorDataList 中提取 CMSensorRecorder。 Apple 尚未提供任何文档。
也许有人给我提示? ;)
func startMovementDetection(){
var accDataList = self.cmSensorRecorder!.accelerometerDataFrom(self.startDate, to: NSDate()) as CMSensorDataList
CMRecordedAccelerometerData() //that's the class i want to extract from CMSensorDataList
}
好的,问题在这里解决了:NSFastEnumeration in Swift
在 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)
}
}
Marcus 在 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)
}
}
这是 Swift 4 的方法。首先,您需要通过扩展使 CMSensorDataList 符合 Sequence:
extension CMSensorDataList: Sequence {
public typealias Iterator = NSFastEnumerationIterator
public func makeIterator() -> NSFastEnumerationIterator {
return NSFastEnumerationIterator(self)
}
}
现在您可以迭代 CMSensorDataList 以获得 CMRecordedAccelerometerData 实例,每个实例由一个 timestamp
和一个 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
}
}
}