NSFastEnumeration in Swift 3

I am trying to iterate over the class object CMSensorDataListreturned CMSensorRecorder.accelerometerData(from:to:). This class confirms the protocol NSFastEnumeration. Therefore, I tried the trick mentioned in https://stackoverflow.com/a/3186168/220 . However, since I am using Xcode Version 8.0 beta (8S128d), it no longer works.

What can I do to make it support loops for-in?

+4
source share
1 answer

In Swift 3, it SequenceTypewas renamed to Sequence(the type suffix was removed from the protocols), generate()was renamed to makeIterator()(the concept of "Generator" was replaced by "Iterator"), and therefore was NSFastGeneratoralso renamed to NSFastEnumerationIterator.

So you want your extension to look like this:

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

All Articles