Created
June 26, 2018 02:03
-
-
Save myssun0325/2c0390decdaf92792412ec85a7097f2b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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