Using Lenses in Swift

It makes things clearer, I guess?

Steven Curtis
5 min readFeb 15, 2023
Photo by Bud Helisson on Unsplash

Before we start

Difficulty: Beginner | Easy | Normal | Challenging

This article covers Lenses in Swift, which are functional getters and setters used to access and modify immutable object. This can be useful when you are working with immutable objects and want to access or modify specific parts of the object without changing the original oor abstract behind getters and setters.

Don’t worry! I’ll take you through this with some examples!

Prerequisites:

What is this about?

This is about Lenses. This is a guide to something that might well help out your functional programming, and help you to become a better programmer.

Now there are two parts to lenses, you might be looking through it to access part of an object (this is the getter). Alternatively we can choose to use a lens to change part of an object which acts like a setter — but crucially we are referring to immutable objects here so a new object will be returned when a change is made.

--

--