struct BoxedFibonacciSequence: Sequence, IteratorProtocol { typealias Element = Int var currentIndex = 0
mutating func next() -> Int? {
defer { currentIndex += 1 }
return loadFibNumber(at: currentIndex)
}
func makeIterator() -> Self {
return self
}
}
func loadFibNumber(at index: Int) -> Int { return fibNumber(at: index) }
var box = BoxedFibonacciSequence() for v in box { print("for index",box.currentIndex) if v < 20 { } else { break } }
let fib = BoxedFibonacciSequence() var iter = fib.makeIterator() while let v = iter.next() { print("while index",iter.currentIndex) if v < 20 { } else { break } }
why use for
index always 0, but while index is normal? I'm confused.