Member-only story
Extensions with Generic Where clauses in Swift
Require the specific element in question to be a specific type.
4 min readJan 21, 2020
Protocols
are extremely important in Swift. Equally Extensions
offer flexibility and allow us to add functionality to both Objects
and Protocols
— but here is an interesting way in which these tools can be combined in Swift.
Difficulty: Beginner | Easy | Normal | Challenging
Prerequisites:
- Be able to produce a “Hello, World!” iOS application (guide HERE)
- Knowledge of extensions in Swift (guide HERE)
- Preferably have some knowledge of Swift’s comparable protocol (guide HERE)
Terminology
Extensions: Extensions add new functionality to a class, struct, enum or protocol
Generics: Reusable functions and types that can work with any type (or subset of a certain type
The example
Imagine we want to have entries on a Leaderboard. In order to do so, we decide to create a Class
to represent each entry on the leaderboard.