100 Days of SwiftUI
2026-04-30
I didn’t really complete Day 12, but I want to continue at least through Day 14 (the end of the introduction to Swift) before I take a break.
Protocols and extensions
Protocol extensions allow us to do away with large, complex inheritance hierarchies, and replaces them with much smaller, simpler protocols that can be combined together.
Will I understand how to implement them better than I do inheritance?
How to create and use protocols
protocol— a blueprint for code that’s going to come later- syntax looks like class inheritance but doesn’t act the same way
- methods that are in the protocol must be in the struct that conforms to it
- protocal is the minimum required functionality for the structs that conform to it
- your struct can conform to more than one protocol at a time
- classes can also use protocols, and subclassing is possible
How to use opaque return types
Honestly I wouldn’t cover it in a beginners course if it weren’t for one very important fact: you will see it immediately as soon as you create your very first SwiftUI project.
- don’t worry yet about how it works, just understand how to use it
- keyword
somemeans the function will return some sort of the type required by the protocol - Swift will always know what the real type of the return data is
- in SwiftUI,
protocol View {}has an opaque return type
when you see
some Viewin your SwiftUI code, it’s effectively us telling Swift “this is going to send back some kind of view to lay out, but I don’t want to write out the exact thing – you figure it out for yourself.”
How to create and use extensions
- keyword
extension— add functionality to any type - not necessarily better than a global function except
- code completion is available to extensions
- don’t have a lot of extra globals hanging around
- can access
privatecomponents in the original type definition - make it easier to modify values in place rather than return a new value
- convention: if returning a new value, name it with a suffix like
-ed(e.g.trimmed()); if modifying in place don’t (e.g.trim()) - extensions can have properties, but only computed properties
How to create and use protocol extensions
- you can extend a protocol
- “by extending the protocol we’re adding functionality that would otherwise need to be done inside individual structs” aka protocol-oriented programming
- at this time, just need to know they exist
A common extension reports isNotEmpty status instead of list.isEmpty == false or !list.isEmpty:
extension Collection {
var isNotEmpty: Bool {
isEmpty == false
}
}Quizzes
- Protocols — 9/12
- Extensions — 10/12 I admit to starting over
- Protocol extensions — 11/12
Checkpoint 8
Make a protocol that describes a building, adding various properties and methods, then create two structs, House and Office, that conform to it. Your protocol should require the following:
- A property storing how many rooms it has.
- A property storing the cost as an integer (e.g. 500,000 for a building costing $500,000.)
- A property storing the name of the estate agent responsible for selling the building.
- A method for printing the sales summary of the building, describing what it is along with its other properties.
I found this one a lot more straightforward than Checkpoint 7
protocol Building {
var name: String { get }
var rooms: Int { get }
var cost: Int { get }
var agent: String { get }
func report()
}
struct House: Building {
var name: String
var rooms: Int
var cost: Int
var agent: String
func report() {
print("The \(name) has \(rooms) bedrooms and costs $\(cost). It is represented by \(agent).")
}
}
struct Office: Building {
var name: String
var rooms: Int
var cost: Int
var agent: String
func report() {
print("The \(name) has \(rooms) offices and costs $\(cost) per square foot. It is represented by \(agent).")
}
}
let flat = House(name: "Flat", rooms: 4,cost: 125_000,agent: "Laura")
flat.report()
let office = Office(name: "Office", rooms: 2, cost: 29, agent:"Joesph")
office.report()Output:
The Flat has 4 bedrooms and costs $125000. It is represented by Laura.
The Office has 2 offices and costs $29 per square foot. It is represented by Joesph.