Override Protocol Associated Type


Welcome to Lesson 3 of Generic Protocols. Nice and easy. You will learn multiples way to override pre-defined associated type.


How to override typed-defined protocol?

Design Protocol

Create a protocol called, FamilyProtocol. It has an associatedtype called familyType in Int.

protocol FamilyProtocol {
  associatedtype familyType = Int
  var familyMembers: [familyType] { get set }

Design Struct

Create a struct that conforms to FamilyProtocol.

struct NumberFamily: FamilyProtocol {
 var familyMembers: [Int] = [1, 2, 3]

let numberFam = NumberFamily() // familyType == Int

Override Associated Type Directly

You may override familyType by creating a typealias in String or any other types.

struct NormalFamily: FamilyProtocol {
  typealias familyType = String
  var familyMembers:  = ["Bob", "Bobby"]

You don't even need to create a typealias. familyType can be modified based on the value.

struct NormalFamily: FamilyProtocol {
  var familyMembers:  = ["Bob", "Bobby"]

Override Associated Type With Generic Struct

Create a generic struct with a type T. The struct conforms to FamilyProtocol.

struct LeeFamily<T>: FamilyProtocol {
  var familyMembers: [T] = []
let family = LeeFamily(familyMembers: ["Bob", "Bobby", "Lee"])


  1. T becomes String based on ["Bob", "Bobby", "Lee"]
  2. familyType becomes String based on T

Source Code



Generic Protocols with Associated Type


You've learned how to override pre-defined associated types in protocols. Yet, it's ironic that you set the type as defined within protocols because it goes against the meaning of generics. Just don't do it.

In the following lesson, you will discover that Protocol Oriented Programming is not the ultimate savior in all cases.

Note: Learn Swift with Bob is available on Udemy. If you wish to receive a discount link, you may sign up here.

results matching ""

    No results matching ""