Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save myssun0325/2c0390decdaf92792412ec85a7097f2b to your computer and use it in GitHub Desktop.
Save myssun0325/2c0390decdaf92792412ec85a7097f2b to your computer and use it in GitHub Desktop.
import Foundation
class SomeClass {
var someList = [1,2,3,4,5,6,7]
}
class SomeClassIterator: IteratorProtocol {
private var someList: [Int]
private var index: Int
init(_ someList: [Int]) {
self.someList = someList
self.index = someList.startIndex
}
func next() -> Int? {
if index < self.someList.endIndex {
defer { index = self.someList.index(after: index) }
return self.someList[index]
} else {
return nil
}
}
}
extension SomeClass: Sequence {
func makeIterator() -> SomeClassIterator {
return SomeClassIterator(self.someList)
}
}
var someClassInstance = SomeClass()
for element in someClassInstance {
print(element)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment