UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

How to make a custom sequence

Swift version: 5.6

Paul Hudson    @twostraws   

When you use a for-in loop, Swift maps that code to a while loop that generates an iterator for your data. Swift then calls next() on that iterator repeatedly until it gets back nil to signal that the loop has ended.

This functionality is all handled through two protocols: Sequence describes something that can be looped over, and IteratorProtocol describes something that iterates over a sequence. If you combine them both into a single type, all you need to do is implement a next() method returning whatever value comes next in your sequence.

Let’s walk through a simple sequence that counts doubles up from 1, so it will count 1, 2, 4, 8, 16, 32, 64, 128, 256, and so on until we run out of integers that can be stored in Int.

In code this means creating a new DoubleGenerator struct that conforms to both Sequence and IteratorProtocol. Because this type conforms to both protocols, we only need to implement one method: next(). This should double the current number then return it, however we need to do that doubling inside a defer block so that we only double after we’ve returned.

Here’s the code:

struct DoubleGenerator: Sequence, IteratorProtocol {
    var current = 1

    mutating func next() -> Int? {
        defer {
            current *= 2
        }

        return current
    }
}

As that’s an infinite sequence you should be careful using it. For example, this code increments a counter each time its loop goes around and exits the loop when the counter reaches 10.

var i = 0

let numbers = DoubleGenerator()
for number in numbers {
    i += 1
    if i == 10 { break }

    print(number)
}

That will print doubles of 1 through to 256.

If you want your iterator and sequences to be different types, you’ll need to add a makeIterator() method to your sequence.

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Available from iOS 8.0 – learn more in my book Swift Design Patterns

Similar solutions…

About the Swift Knowledge Base

This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.

BUY OUR BOOKS
Buy Pro Swift Buy Pro SwiftUI Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

Average rating: 3.0/5

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.