SOLID Principles for iOS Apps
SOLID is a group of principles that lead you to write clear and organized code without additional effort. Learn how to apply it to your SwiftUI iOS apps. By Ehab Amer.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
SOLID Principles for iOS Apps
40 mins
- Getting Started
- Understanding SOLID’s Five Principles
- Single Responsibility
- Open-Closed
- Liskov Substitution
- Interface Segregation
- Dependency Inversion
- Auditing the Project
- Invoking the Single Responsibility Principle
- Using the New Persistence
- Implementing the Open-Closed Principle
- Creating the Enum
- Cleaning up the Reports
- Updating ContentView.swift
- Adding Weekly Reports
- Applying Dependency Inversion
- Removing the Core Data Dependency
- Seeing Your Changes in Action
- Simplifying the Reports Datasource Interface
- Refactoring ExpensesView
- Adding Interface Segregation
- Splitting up Protocols
- Implementing Liskov Substitution
- Auditing the App Again
- Where to Go From Here?
Auditing the App Again
Take another look at your app. With the changes you made, you resolved all the issues you identified in the first round:
- Core Data setup is no longer in
AppMain
and you separated it. - Your app doesn't rely on Core Data. It can now freely use any kind of storage with minimal changes to your code.
- Adding new report types is a matter of adding a new value in an
enum
. - Creating previews and tests is a lot easier than before, and you no longer need any complex mock objects.
There's a big improvement between how the project was before you started and how it is now. It didn't require much effort, and you reduced the amount of code as a side benefit.
Following SOLID isn't about executing a set of rules or about the setup of your architecture. Rather, SOLID gives you some guidelines to help you write code in a more organized way.
It makes fixing bugs safer since your objects aren't entangled. Writing unit tests is easier. Even reusing your code from one project to another is effortless.
Writing clean and organized code is a goal that always pays off. If you say, "I'll clean this up later", when that later moment comes, things will usually be too complicated to actually clean.
Where to Go From Here?
You can download the completed project files by clicking the Download Materials button at the top or bottom of the tutorial.
Using design patterns in your code offers simple solutions to what seems like a complex problem. Whether you know the fundamental iOS design patterns or not, it's always good to refresh your memory about them. Our Fundamental iOS Design Patterns tutorial can help.
Unit testing is a critical aspect of software development. Your tests need to focus on small parts of your code. Learn all about Dependency Injection to write great unit tests.
Another interesting concept that would improve the way you think about writing your apps is Defensive Programming. It's about having your code anticipate what might go wrong so your app isn't fragile and doesn't crash when it receives unexpected input.
A simple example of defensive coding is using guard let
instead of force unwrapping when you deal with optionals. Being aware of such topics improves the quality of your work without any additional effort.
We hope you enjoyed this tutorial. If you have any questions or comments, please join the forum discussion below!