Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSFastEnumeration in Swift 3

I am trying to iterate over a object of CMSensorDataList class returned by CMSensorRecorder.accelerometerData(from:to:). This class confirms to NSFastEnumeration protocol. So I tried the trick mentioned in https://stackoverflow.com/a/25872991/5603109. However since I am using Xcode Version 8.0 beta (8S128d), it no longer works.

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

like image 232
quarterest Avatar asked Dec 18 '22 16:12

quarterest


1 Answers

In Swift 3, SequenceType has been renamed to Sequence (the "Type" suffix has been removed from protocols), generate() has been renamed to makeIterator() (the concept of a "Generator" has been replaced by an "Iterator") and therefore NSFastGenerator has also been renamed to NSFastEnumerationIterator.

Thus you'll want your extension to look like this:

extension CMSensorDataList : Sequence {
    public func makeIterator() -> NSFastEnumerationIterator {
        return NSFastEnumerationIterator(self)
    }
}
like image 166
Hamish Avatar answered Feb 16 '23 00:02

Hamish