December 2018
Intermediate to advanced
414 pages
10h 19m
English
When using Range<Int> or ClosedRange<Int>, in addition to others, the Standard Library provides conditional conformance to the Sequence type, as follows:
extension Range: Sequence where Bound: Strideable, Bound.Stride : SignedInteger { public typealias Element = Bound public typealias Iterator = IndexingIterator<Range<Bound>>}
Extract from Swift Core source code (https://github.com/apple/swift/blob/master/stdlib/public/core/Range.swift).
This means that you can use this range as the source for iterating on it, as shown in the following example:
let doubles = (1..<10).map { $0 * 2 } // [2,4,6,8,10,12,14,16,18]for i in 1..<10 { // do something with i}
Read now
Unlock full access