Using Lenses in Swift
It makes things clearer, I guess?
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:
- It would be really useful if you has a good idea about both getters and setters and their use in Swift
- It would be helpful if you had a good grasp of generics in Swift
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.