NSFastEnumeration in Swift 3

僤鯓⒐⒋嵵緔 提交于 2019-12-23 12:17:21

问题


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?


回答1:


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)
    }
}


来源:https://stackoverflow.com/questions/37829953/nsfastenumeration-in-swift-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!